function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdcChi2sfdcChi2 

lookupfield update with a trigger

Can anyone help with a sample code that can autopopulate a lookup field from a text field on the same object (lead) upon creation

Both the text field and the lookup field are on the lead object. While creating a new lead, we have the value of the text field written from an external source and we want to write that same value into the lookup field.

thank you
Best Answer chosen by sfdcChi2
sfdcChi2sfdcChi2
trigger LeadITO_Trigger on Lead (before insert, before update) {
   
    set<string>toupdate = new set<string>();
    for (lead l : trigger.new){
        if (l.externalSource__c!=NULL)
        toupdate.add(l.externalSource__c);
    }
   
    lMAccountLookup__c rvp =[ select id, Name from lMAccountLookup__c where Name=:toupdate];
   
    for (lead l : trigger.new){
        l.lMAccountLookup__c = rvp.id;
    }
   
   

}

thanks guys for pointing me in the right direction. this worked for me

All Answers

Shyam BhundiaShyam Bhundia
Hello, 

Can you give more details on your data model please?  What is the lookup field related to?


sfdcChi2sfdcChi2
it's a lookup field to a custom object call LMAccounts.  the object holds the names of partner accounts
Shyam BhundiaShyam Bhundia
how is the external source field linked to the LMAccounts?  What I'm trying to get at is, how do we know what LMAccount links to what value in the external source field?
sfdcChi2sfdcChi2
the name on the text field will be one of those in the LMObject. However we need to write it into the LMAccounts lookup field in order to link the particular lead record to the LMAccount object. 
hope my explanation is clear enough.
thanks
sfdcChi2sfdcChi2
the external source is not linked to the LMObject. it simply creates a lead record and writes a value(an account name) into a field called partners.

however because it's a text field, it doesnt tie the lead to the LMObject. we need a lookup field for that.

hence we have a look up field called LMAccounts on the lead object that points to the LMObject. and i want to write the account name in the partner field to the lookup field to create that relationship
sbbsbb
You can write a trigger (insert/update) where you query the LMAccounts object using the value in your text field. If a match is found, put the ID of the matching LMAccounts record into the lookup field. If more than one match is found, you will need to determine which record to link to. If no match is found, you will need to either leave the lookup field blank (if it is not required) or raise an error.
Shyam BhundiaShyam Bhundia
ok makes sense now...

First you need to query for the LMAccount with the name matching the value in the external source field (lets say that field is called lead.externalSource__c)

LMAccount__c relatedLMAccount = [select id FROM LMAccounts WHERE name = : lead.externalSource__c Limit 1];
The above query assumes that ho e names on the LMAccounts are uniqiue.

Once you have relatedLMAccount, you need check if a record was found, if it was then assign it to the lookup field (lead.lMAccountLookup__c)

if(null != relatedLMAccount){
     lead.lMAccountLookup__c = relatedLMAccount.id;
}

You probably would want to do this on a before insert trigger.

Hope this helps.
sfdcChi2sfdcChi2
Thanks guys, I will give this a shot now and if i face any problems will revert to you guys. really appreciate your help
sfdcChi2sfdcChi2
trigger LeadITO_Trigger on Lead (before insert, before update) {
   
    set<string>toupdate = new set<string>();
    for (lead l : trigger.new){
        if (l.externalSource__c!=NULL)
        toupdate.add(l.externalSource__c);
    }
   
    lMAccountLookup__c rvp =[ select id, Name from lMAccountLookup__c where Name=:toupdate];
   
    for (lead l : trigger.new){
        l.lMAccountLookup__c = rvp.id;
    }
   
   

}

thanks guys for pointing me in the right direction. this worked for me
This was selected as the best answer