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
James DurkeeJames Durkee 

Trigger that copies data from a Text field to a lookup field.

Hey All,  I'm new to APEX development and Saleforce and attempting to setup the following trigger.

We collect data on our W2L form as a text field:  ZipCode__c

The zip code in this field needs to then be copied to a lookup field that's a database object of zip codes, cities and states.   Another trigger runs after that to populate the city/state on the lead record.   The 2nd trigger is working fine, but i'm not getting anything with the first trigger.

Here's the code, it compiles with no errors:

trigger ZipCodeLookupCopy on Lead (before insert, before update) {
   
    set<ID>cObjectID = new set<ID>();    
   
    for(Lead l : Trigger.new){
        if(l.address1_zip__c != null){
            cObjectID.add(l.address1_zip__c);
        }
    }
   
    if(!cObjectID.isEmpty()){
         
        Map<ID,zipLookup__c> cObjectMap = new Map<ID,zipLookup__c>([select Id, Name from zipLookup__c where Id IN: cObjectID]);
       
        for(Lead l : trigger.new){            
            if(cObjectMap.get(l.address1_zip__c).name != Null){
                // fill the Zip Code on Lead with Zip Code on address1_zip__c
                l.ZipCode__c = cObjectMap.get(l.address1_zip__c).name;
            }
        }
    }
}



 

Hargobind_SinghHargobind_Singh
Hi, I see two problems:

1. The set in where-clause is being checked against zip-code, where-as your select where clause is searching against ID. 
2. Your map on line 13 has "ID" as the key, where-as in your loop below, on line 16 and 17, you are trying to search with Zip-Code. 

I would suggest you change your map code to (assuming that "name" field on ziplookup__c contains the zipcode)  :

Map<String, zipLookup__c> cObjectMap = new Map<String, zipLookup__c>();
for(zipLookup__c zip: [select Id, Name from zipLookup__c where Name IN: cObjectID]){
	cObjectMap.put(zip.name, zip); 
}


Hargobind_SinghHargobind_Singh
Also, by the way, if your other trigger is a before trigger as well, you might face a problem. Salesforce doesn't guarantee sequence of executing of triggers in same event. You should try to club functionality into one trigger, else there is a chance that your other trigger runs first, and finds nothing in lookup, and your other trigger runs after that and fills the lookup. 


logontokartiklogontokartik
Hey James, looks like you got your ids and names confused. 

So from what I see address1_zip__c is a lookup field which will have an lookup to Ziplookup Object? Zipcode__c is an actual value like '02481' If its true then your code must be 

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

    Set<String> czipcodes = new Set<String>():   

    for(Lead l : Trigger.new){
        if(l.ZipCode__c != null){
            czipcodes.add(l.ZipCode__c);
        }
    }
   
    if(!czipcodes.isEmpty()){
         
        Map<String,zipLookup__c> cObjectMap = new Map<String,zipLookup__c>();
        for(zipLookup__c zl : [select Id, Name from zipLookup__c where Name IN: czipcodes]){
          cObjectMap.put(zl.Name,zl);
        }
       
        for(Lead l : trigger.new){            
            if(cObjectMap.containsKey(l.ZipCode__c)){
                // fill the Zip Code on Lead with Zip Code on address1_zip__c
                l.ZipCode__c = cObjectMap.get(l.ZipCode__c).Id;
            }
        }
    }
}


Let me know if this works?