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 

Apex Trigger to update a lookup field

Ineed help withthe following Apex Trigger:

trigger ForceSourceCrossRef on Lead (before insert, before update ) {
   
    For (Lead l:Trigger.new) {        
        if(l.Source__c != null) {
           List<CrossRef__c> cr = new List<CrossRef__c>();
           cr = [Select Id from CrossRef__c WHERE
                   CrossRef__c.Name = :l.Source__c ORDER BY Id LIMIT 1];
              if(cr.size()>0){
                    cr.Source_Cross_Reference__c = cr[0].Id;
            }
        }
    }    
}

Attempting to update a custom lookup field on the Lead object called Source_Cross_Reference__c with the value that's imported in a Lead field called Source__c.  (Source__c is a picklist field.)  There is a custom object called CrossRef__c that uses the Source__c picklist value in the CrossRef__c.Name field.  I want to populate the Source_Cross_Reference__c field with the ID value from the CrossRef__c object.  The Devleoper Console is displaying the following Problem: "Initial term of field expression must be a concrete SObject: List&lt;CrossRef__c&gt;".. What is neededto correct this problem?  Will this trigger perform the update described earlier?
Best Answer chosen by Gary Payne
Sumitkumar_ShingaviSumitkumar_Shingavi
Hey Gary,

I think I misunderstood your field and objects. Here you go with new code revision:
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, Source_Cross_Reference__c 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;				
			}
        }
    }	
}
Hope this works as per your expectations
 

All Answers

Sumitkumar_ShingaviSumitkumar_Shingavi
You need to change the code to make it bulkified. I corrected your code and you can try it.
 
trigger ForceSourceCrossRef on Lead (before insert, before update ) {
   
	List<CrossRef__c> lCRs = new List<CrossRef__c>();
	
	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)) {
				CrossRef__c crTemp = mCrossRefs.get(l.Source__c);
				crTemp.Source_Cross_Reference__c = crTemp.Id;
				lCRs.add(crTemp);
			}
        }
    }
	
	if(!lCRs.isEmpty()) update lCRs;
}

Hope this helps! if yes, then mark it as solution.

Thanks,
Sumit
 
sailaja majji 5sailaja majji 5
Hi Gary ,

 Will this trigger perform the update described earlier ? 

A.You have to update the field on lead object not the CrossRef__c object .This is how you should do .trigger ForceSourceCrossRef on Lead (before insert, before update ) {
   
    For (Lead l:Trigger.new) {        
        if(l.LeadSource != null) {
           List<CrossRef__c> cr = new List<CrossRef__c>();
           cr = [Select Id from CrossRef__c WHERE
                   CrossRef__c.Name = :l.LeadSource ORDER BY Id LIMIT 1];
              if(cr.size()>0){
                    l.Source_Cross_Reference__c = cr[0].Id;
            }
        }
    }    
}

This code needs ot be bulkified .

Thanks,
Sailaja
Gary PayneGary Payne
Thanks Sumit! I am receiving the following Problem in the Developer Console: “Invalid field Source_Cross_Reference__c for SObject CrossRef__c”. The field Source_Cross_Reference__c is a Lead object custom lookup field that should be updated with the ID from the CrossRef__c object. Any ideas? Regards, Gary Payne, CRM Administrator/Information Technology SWBC 9311 San Pedro Ave., Suite 600 San Antonio, TX 78216 888-391-6707 - Toll Free/ Help Desk 210-376-6057 - Direct 714-474-5915 - Mobile 210-468-4135 - Fax [cid:image001.png@01D0575E.271DC870] [Description: Description: Description: Description: facebook-official-28px] [Description: Description: Description: Description: linkedIn-official-28px] [Description: Description: Description: Description: youTube-sm] [Twitter_logo_email] [cid:image007.png@01D0575E.271DC870] Visit our website at www.swbc.com
surasura
try below code , replace your trigger with it ,
trigger ForceSourceCrossRef on Lead (before insert, before update ) {

   List<String> crossrefIds = new Set<String>();
   Map<String,CrossRef__c> NameToCrossRef = new Map<String,CrossRef__c>(); 
  for (Lead l :trigger.new )
  {
      if(l.Source__c != null) 
      {
          crossrefIds.add(l.Source__c );
      }


  }
  for (CrossRef__c cr = [Select Id,Name from CrossRef__c WHERE Name IN :crossrefIds  ORDER BY Id])
  {
         NameToCrossRef .put(cr.Name,cr);
  }
   
  for (Lead l :trigger.new )
  {
      if(l.Source__c != null) 
      {
         CrossRef__c cr  = NameToCrossRef .get(l.Source__c );
         if(cr != null )
         {
                l.Source_Cross_Reference__c = cr.Id;

         }
      }


  }



  
}
Gary PayneGary Payne
Thanks Surs Sura, I tried the trigger you sent and am receiving the following Problem on the Developer Console: “ Line 14: expecting a semi-colon, found ')'” Following is line 14 code: for (CrossRef__c cr = [Select Id,Name from CrossRef__c WHERE Name IN :crossrefIds ORDER BY Id]) { What can fix this? Regards, Gary Payne, CRM Administrator/Information Technology SWBC 9311 San Pedro Ave., Suite 600 San Antonio, TX 78216 888-391-6707 - Toll Free/ Help Desk 210-376-6057 - Direct 714-474-5915 - Mobile 210-468-4135 - Fax [cid:image001.png@01D0575E.271DC870] [Description: Description: Description: Description: facebook-official-28px] [Description: Description: Description: Description: linkedIn-official-28px] [Description: Description: Description: Description: youTube-sm] [Twitter_logo_email] [cid:image007.png@01D0575E.271DC870] Visit our website at www.swbc.com
Sumitkumar_ShingaviSumitkumar_Shingavi
Hey Gary,

I think I misunderstood your field and objects. Here you go with new code revision:
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, Source_Cross_Reference__c 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;				
			}
        }
    }	
}
Hope this works as per your expectations
 
This was selected as the best answer
surasura
sorry try this 
 
trigger ForceSourceCrossRef on Lead (before insert, before update ) {

   List<String> crossrefIds = new Set<String>();
   Map<String,CrossRef__c> NameToCrossRef = new Map<String,CrossRef__c>(); 
  for (Lead l :trigger.new )
  {
      if(l.Source__c != null) 
      {
          crossrefIds.add(l.Source__c );
      }


  }
  for (CrossRef__c cr : [Select Id,Name from CrossRef__c WHERE Name IN :crossrefIds  ORDER BY Id])
  {
         NameToCrossRef .put(cr.Name,cr);
  }
   
  for (Lead l :trigger.new )
  {
      if(l.Source__c != null) 
      {
         CrossRef__c cr  = NameToCrossRef .get(l.Source__c );
         if(cr != null )
         {
                l.Source_Cross_Reference__c = cr.Id;

         }
      }


  }



  
}

 
surasura
hey go to top and check the  the code, u just posted a rubbish code even without reading the question , my code was missing : instand of = so i corrected it you  it seems you are the one who is trying to increase the post count 
surasura
sure you are true profuessional who answers questions without reading them LOL , and cant understand difference between two codes
Gary PayneGary Payne
Thank you Sumitkumar, The revised Apex Trigger you sent worked without any problems. Now I just need to create an Apex Test Class. Warmest Regards, Gary Payne, CRM Administrator/Information Technology SWBC 9311 San Pedro Ave., Suite 600 San Antonio, TX 78216 888-391-6707 - Toll Free/ Help Desk 210-376-6057 - Direct 714-474-5915 - Mobile 210-468-4135 - Fax [cid:image001.png@01D0575E.271DC870] [Description: Description: Description: Description: facebook-official-28px] [Description: Description: Description: Description: linkedIn-official-28px] [Description: Description: Description: Description: youTube-sm] [Twitter_logo_email] [cid:image007.png@01D0575E.271DC870] Visit our website at www.swbc.com
Gary PayneGary Payne
Best Answer is marked! Gary Payne, CRM Administrator/Information Technology SWBC 9311 San Pedro Ave., Suite 600 San Antonio, TX 78216 888-391-6707 - Toll Free/ Help Desk 210-376-6057 - Direct 714-474-5915 - Mobile 210-468-4135 - Fax [cid:image001.png@01D0575E.271DC870] [Description: Description: Description: Description: facebook-official-28px] [Description: Description: Description: Description: linkedIn-official-28px] [Description: Description: Description: Description: youTube-sm] [Twitter_logo_email] [cid:image007.png@01D0575E.271DC870] Visit our website at www.swbc.com