You need to sign in to do that
Don't have an account?
James 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; } } } }
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) :
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
Let me know if this works?