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
BobbyQBobbyQ 

Trigger to update lookup field with related object ID

Hello, trying to piece together my first trigger and could use some help? I have a feeling it something simple I'm overlooking.

I setup a custom object 'Territory_Zip_Codes__c', where the name is 'Zip Code'. The Lead has a lookup field 'Territory_Zip_Code__c'. When 'postalcode' is entered or edited on the Lead I want to update the 'Territory_Zip_Code__c' lookup field on the lead. When I sub in a random text field on the Lead in place of 'Territory_Zip_Code__c', I can see it get updated with the proper ID from the custom object. But, when I point it back to the lookup field it gives this error when updating the record:
 
Territory: execution of BeforeUpdate caused by: System.StringException: Invalid id: ()
trigger Territory on Lead (before insert, before update) { 
    set<string> left5zips = new set<string>(); 
    for (lead l: trigger.New) {
        if (l.postalcode != null) {
            left5zips.add(l.postalcode);
        }else {
            l.Territory_Zip_Code__c='None';   
        }
    }

    //query the zip_code object to get the zipcode (Name) and ID from the zip code object 
    map<string,string> zMap=new map<string,string>(); 
    for(Territory_Zip_Codes__c z : [Select name, ID from Territory_Zip_Codes__c WHERE name IN :left5zips]) {
        zMap.put (z.name, z.ID);
    }

    for(lead l:trigger.new){ 
        if(l.postalcode != null){ 
            if(zmap.containskey(l.postalcode)){
               l.Territory_Zipcode__c= '';
                l.Territory_Zipcode__c= zmap.get(l.postalcode);
                if(l.Territory_Zipcode__c!= null && l.Territory_Zipcode__c!= ''){
                    l.Territory_Zipcode__c= l.Territory_Zipcode__c; 
                }
            }           
        }
    } 
}

 
ANUTEJANUTEJ (Salesforce Developers) 
Hi Britton,

Can you elaborate the scenario with an example also when you mentioned "But, when I point it back to the lookup field it gives this error when updating the record" do you mean that on changing the lookup field value to the previous one you are getting the above-stated error?

Looking forward to your response.

Thanks.
BobbyQBobbyQ

Hi ANUTEJ, thanks so much for the reply. Yes, happy to elaborate.

A lead record has a postalcode of '96051'. Also on the lead record is a lookup field related to a custom object called 'Territory_Zip_Codes__c', that has a list of all US zip codes along with additonal columns identifying territory values. The name field on this custom object are the zip code values.

When the postal code on this lead record is entered or udpated, to '96051' in this example. I want to populate the lookup field with the ID 'aMD4N0000000A9t' of the corresponding zip code, '96051', from the custom object. Once the lookup field is populated I have other formulas and workflows that get triggered to update other fields.

What I meant about pointing to another field was I substitued in a random text field in place of the lookup field 'l.Territory_Zipcode__c' just to troubleshoot and confirm the ID 'aMD4N0000000A9t' was the output value. It was, the text field showed 'aMD4N0000000A9t'. However, when I switched the trigger back to the lookupfield it gives the error 'Territory: execution of BeforeUpdate caused by: System.StringException: Invalid id: ()'.

I'm not sure, but if I had to guess it is because something is off in this section. Should it be something like 'map<string,ID> zMap=new map<string,ID>'? or perhaps the select should be something like '[Select ID, from Territory_Zip_Codes__c WHERE ID IN :left5zips]'. Or maybe I am just completely off?

map<string,string> zMap=new map<string,string>(); 
    for(Territory_Zip_Codes__c z : [Select name, ID from Territory_Zip_Codes__c WHERE name IN :left5zips]) {
        zMap.put (z.name, z.ID);