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
Josh HarshJosh Harsh 

Trigger on Lookup Field to Match against Zip/Postal Code

I am hoping someone can help me out with this, I have a custom object called Zip Code and it stores all Zip Codes within the United States. I would like to create a trigger that takes the first 5 digits of a US zip code and places it in a lookup field matching that 5 digit billing zip code.

I do not really have much knowledge with apex and was hoping someone would be able to point me in the right direction or possibly someone has already accomplished this sames situation.

Thank you for your help!
Ramu_SFDCRamu_SFDC
To havethe zipcode as the lookup field, you would need to have the name field of custom object store zip code. This way you can search for the zipcode to display all the zip code records that match these 5 digits.
Josh HarshJosh Harsh
Currently that is how it is setup, the name field is the 5 digit zip code.
Ramu_SFDCRamu_SFDC
Can you please elaborate your requirement at this point.
Josh HarshJosh Harsh
Yes, I would like to have a apex trigger that takes the first five digits of the Billing Zip/Postal Code on an account page and uses that to populate the lookup field "Zip Code Territory." That look up field goes to an object with all Zip codes in the United States in 5 digit format. The goal is to not have the user updating the Zip code twice, so if we can use the Zip/Postal Code to look up the matching Zip Code ID from the custom object to then populate the lookup field.

Please let me know if that is unclear in anyway.

Thanks!
Ramu_SFDCRamu_SFDC
Hi John, let me re-iterate your requirement, You have a custom object zip code that holds all the zip code records in it. You want an apex trigger to be implemented such that when an account record is created or updated, you want the first 5 digits of the billing zip or postal code to be captured and searched across the zip code database(custom object records) to update the Zip/Postal code lookup field on the account.

below is the trigger that you may use

trigger updatezipcode on Account(before insert, before update){
map<string,id> zipcodes=new map<string,id>();
for(zipcode__c zipcode:select id,name from zipcode__c){
zipcodes.put(zipcode.name,zipcode.id);
}

for(Account acc:trigger.new){

string zipcode1=acc.zipcode__c,substring(1,6);  //zip/postal code of accouunt
if(zipcodes.containskey(zipcode1){
acc.zipcode__c=zipcodes.get(zipcode1);
}
}

}


Josh HarshJosh Harsh
This is really helpful! Although it gives me an error on line 3 "unexpected token: 'select'." I am very unfamiliar with apex so this may be asking to much, but if the Zip Code object "Zip_Code__c" the zip code on that object "Name" the Zip Code Id "Id" and the Billing Zip/Postal Code "Account.BillingPostalCode" how does that translate into the code?

I thank you very much for your help in advance!
Ramu_SFDCRamu_SFDC
My bad the select query should have been wrapped within square brackets. Please review the below changed code as per your field name specifications

trigger updatezipcode on Account(before insert, before update){
map<string,id> zipcodes=new map<string,id>();
for(Zip_Code__c zipcode:[select id,name from Zip_Code__c]){
zipcodes.put(zipcode.name,zipcode.id);
}

for(Account acc:trigger.new){

string zipcode1=acc.BillingPostalCode.substring(1,6);  //zip/postal code of account truncated to get first 5 characters starting at 1st character and ending at 6th
if(zipcodes.containskey(zipcode1){
acc.BillingPostalCode=zipcodes.get(zipcode1);
}
}

}





Josh HarshJosh Harsh
Name specs are perfect! But I am receiving the following error when I save the Account: 
Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger updatezipcode caused an unexpected exception, contact your administrator: updatezipcode: execution of BeforeInsert caused by: System.StringException: Ending position out of bounds: 6: Trigger.updatezipcode: line 9, column 1
Do you know what may be causing that?
Josh HarshJosh Harsh
I forgot to mention the name of the lookup field it is going to. It is Zip Code Territory "Account.Zip_Code_Territory__c" I am not sure if that makes a difference?
Josh HarshJosh Harsh
I have made the following changes in order to update the Zip Code Territort field on the Account.

trigger updatezipcode on Account(before insert, before update){
map<string,id> zipcodes=new map<string,id>();
for(Zip_Code__c zipcode:[select id,name from Zip_Code__c]){
zipcodes.put(zipcode.name,zipcode.id);
}

for(Account acc:trigger.new){

string zipcode1=acc.BillingPostalCode.substring(1,6);  //zip/postal code of account truncated to get first 5 characters starting at 1st character and ending at 6th
if(zipcodes.containskey(zipcode1)){
acc.Zip_Code_Territory__c=zipcodes.get(zipcode1);  //Update the Zip Code Territory field with the first 5 characters of the Billing Postal Code
}
}

}

But I am still receiving the same error as mentioned above.
Vijaya Kumar RegantiVijaya Kumar Reganti
Hi,

Your string stores only 5 letters, but you are trying to access the 6th one.That's why it is giving you the List out of bound exception.
Please use string zipcode1=acc.BillingPostalCode.substring(1,5);   instead of string zipcode1=acc.BillingPostalCode.substring(1,6);

Thanks,
Vijay

Vijaya Kumar RegantiVijaya Kumar Reganti
Hi,

Your string stores only 5 letters, but you are trying to access the 6th one.That's why it is giving you the List out of bound exception.
Please use string zipcode1=acc.BillingPostalCode.substring(0,5);   instead of string zipcode1=acc.BillingPostalCode.substring(1,6);

Thanks,
Vijay

Josh HarshJosh Harsh
I was able to finally get it to work, I re wrote the code, but now I need to make sure that it just pulls from the first 5 characters of the billing zip code. Example if the Billing Zip Code is 99999-9999 it only needs to look at the first 99999.

trigger updatezipcode on Account (before insert, before update) {
    List<String> BillingPostalCodes = new List<String>();
   
    for (Account a:Trigger.new){
        BillingPostalCodes.add(a.BillingPostalCode);
    }

    List <Zip_Code__c> ZipCodeList = [Select ID, Name from Zip_Code__c where Name in :BillingPostalCodes];

    for (Integer i = 0; i <Trigger.new.size(); i++){
        if (ZipCodeList.size() > 0 && Trigger.new[i].BillingPostalCode !=null){
                        for (Zip_Code__c z:ZipCodeList){
                if (Trigger.new[i].BillingPostalCode == z.name){
                        Trigger.new[i].Zip_Code_Territory__c = z.ID;
                                }
                        }
        }
        else{
        Trigger.new[i].Zip_Code_Territory__c = null;
        }
       
    }
}

So i would like to incorporate "string zipcode1=acc.BillingPostalCode.substring(1,5);" but am not sure where to place it.