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
Gary PayneGary Payne 

Populate Lead lookup field with a Lead text field value

I need a Lead trigger to populate a Lead lookup field (Financial_Institution_Name__c) with the value from a Lead text field (CU_IP__c).  The lookup field is tied to a custom object called FI_Ref__c, where the key field is the Financial Institution Name.  The CU_IP__c text field contains a FI Name value that will match up to the FI_Ref__c values in its Financial Institution Name field.  The CU_IP__c field value is populated automatically when a new Lead record is populated via a Dataloader process.  The import process provides a Source code from the Credit Union that supplied the Lead record. That Source code is used to determine the value that is placed in the CU_IP__c field.  I need to copy the CU_IP__c field value and have it update into the Financial Institution Name field when the Financial Institution Name field is null.  
ShashankShashank (Salesforce Developers) 
Here's a sample trigger for your purpose:
 
trigger lookupFI on Lead (after insert) {
    set<string> fiset = new set<string>();
    list<lead> leadsToUpdate = new list<lead>();
    for(lead l:trigger.new){
        string fi = l.CU_IP__c;
        fiset.add(fi);
    }
    map<string,FI_Ref__c> fimap = new map<string,FI_Ref__c>();
    list<FI_Ref__c> filist = [select Id, Name from FI_Ref__c where Name IN :fiset];
    for(FI_Ref__c f:filist){
        for(string s:fiset){
            if((s!=null)&&(f.Name!=null)){
                if(s==f.Name){
                    fimap.put(s,f);
                }
            }
        }
    }
    for(lead le:trigger.new){
        if(le.Financial_Institution_Name__c==null){
            if(le.CU_IP__c!=null){
                lead lea = new lead();
                lea.Id = le.Id;
                lea.Financial_Institution_Name__c = fimap.get(le.CU_IP__c).Id;
                leadsToUpdate.add(lea);
            }
        }
    }
    if(leadsToUpdate.size()>0){
        update leadsToUpdate;
    }
}

 
Gary PayneGary Payne
Thanks for the Apex Trigger Shashank.  I saved it as a Lead Trigger to my test sandbox but it doesn't do anything.  What can I do in the Developer Console to see if it can work?  The CU_IP__c field is populated after the Lead record is created from a combination of another Lead Trigger (see ForceSourceCrossRef below) and an update workflow (LeadSource2CU - Source_Cross_Reference__r.External_Unique_Id__c.  The External_Unique_Id__c field is the name of the Credit Union that matches up with the Lead.Source__c field value, which is a code.

trigger ForceSourceCrossRef on Lead (before insert, before update ) {
       
  Set<String> sSources = new Set<String>();
  for(Lead l : Trigger.new) {        
    if(l.Source__c != null) sSources.add(l.Source__c);
  }
  
  Map<String, CrossRef__c> mCrossRefs = new Map<String, CrossRef__c>();  
  for(CrossRef__c crInstance : [SELECT Id, Name FROM CrossRef__c WHERE Name IN :sSources]) {
    mCrossRefs.put(crInstance.Name, crInstance);
  }
  
    for(Lead l : Trigger.new) {        
        if(l.Source__c != null) {
            if(mCrossRefs.containsKey(l.Source__c)) {        
        l.Source_Cross_Reference__c = mCrossRefs.get(l.Source__c).Id;        
      }
        }
    }  
}

Any ideas you may have will be helpful.
ShashankShashank (Salesforce Developers) 
In that case, you may have to add "after update" event as well to the triggger. Try changing the first line of the trigger to this:
 
trigger lookupFI on Lead (after insert, after update) {