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
OnurKOnurK 

Updating Lookup Field

Hi everyone,

 

I have US university names stored in Accounts. I also collect information from web-to-lead, "university name"  is a drop down comes as a text field from web site to lead. Landing field in Leads is called 'University Name". (University_name__c) I would like to update lookup field "University" in Leads (University__c) (which is a lookup and connected to Accounts) by using the text value from University_name__c. Here is the code I am working with. I appreciate a lot if anyone can help.

 

trigger LeadAccount on Lead (before insert, before update) {

   

List<String> LeadUni = new List<String>();
   

    //University_name__c is a text field in Leads

   

    for(Lead ld : trigger.new)
    if(ld.University_name__c != null)
    LeadUni.add(ld.University_name__c);

 
    //AccountMap stores Account id from University_name__c field because Account.Name = Lead.University_name__c.

     
    Map<String, Account> AccountMap = new Map<String, Account>(
    [SELECT id from Account where name IN: LeadUni]);
    
  

    for(Lead ld : Trigger.new)

    ld.University__c = AccountMap.get(ld.University_name__c).id;
   
   }

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
OnurKOnurK

Hi Eprasu,

 

Your solution was great, I appreciate a lot for the response. I made a little change with the last part of the code and it works. I am going to post the correct code and people can use it.

 

Thanks again.

 

 

trigger LeadAccount on Lead (before insert, before update) {
 
    
    Set<String> setUniversityName = new Set<String>();
    
    for(Lead ld : trigger.new)
    if(ld.University_Name__c != null && !setUniversityName.contains(ld.University_Name__c))
        setUniversityName.add(ld.University_Name__c);
 
         
    Map<String, Account> AccountMap = new Map<String, Account>([select Id,Name from Account where Name in :setUniversityName]);
    Map<String,Id> mapNameIdAcc = new Map<String,Id>();
    
    for(Account account : AccountMap.values())
        mapNameIdAcc.put(account.Name , account.Id);
  
    for(Lead ld : Trigger.new)
    ld.University__c = mapNameIdAcc.get(ld.University_Name__c);
   
   }

 

 

All Answers

_Prasu__Prasu_

Following is the rough drafted code. I have not tried to execute it. Let me know if you find any issues.

 

 

trigger LeadAccount on Lead (before insert, before update) {
 
    
    Set<String> setUniversityName = new Set<String>();
   
    for(Lead ld : trigger.new)
    if(ld.University_name__c != null && !setUniversityName.contains(ld.University_name__c))
    	setUniversityName.add(ld.University_name__c);
 
         
    Map<String, Account> AccountMap = new Map<String, Account>([select Id,Name from Accounts where Name in :setUniversityName]);
    Map<String,Id> mapNameIdAcc = new Map<String,Id>();
    
    for(Account account : AccountMap.values())
    	mapNameIdAcc.put(account.Name , account.Id);
  
    for(Lead ld : Trigger.new)
    ld.University__c = AccountMap.get(ld.University_name__c);
   
   }

 

 

OnurKOnurK

Hi Eprasu,

 

Your solution was great, I appreciate a lot for the response. I made a little change with the last part of the code and it works. I am going to post the correct code and people can use it.

 

Thanks again.

 

 

trigger LeadAccount on Lead (before insert, before update) {
 
    
    Set<String> setUniversityName = new Set<String>();
    
    for(Lead ld : trigger.new)
    if(ld.University_Name__c != null && !setUniversityName.contains(ld.University_Name__c))
        setUniversityName.add(ld.University_Name__c);
 
         
    Map<String, Account> AccountMap = new Map<String, Account>([select Id,Name from Account where Name in :setUniversityName]);
    Map<String,Id> mapNameIdAcc = new Map<String,Id>();
    
    for(Account account : AccountMap.values())
        mapNameIdAcc.put(account.Name , account.Id);
  
    for(Lead ld : Trigger.new)
    ld.University__c = mapNameIdAcc.get(ld.University_Name__c);
   
   }

 

 

This was selected as the best answer