You need to sign in to do that
Don't have an account?
Eric Grammer
Left Function in Apex Trigger
Hi, I am looking for the correct function to "scrape" the 1st 5 digits of the billing postal code to use in my exisitng apex trigger to populate a lookup filed of "PostalCode" on my account records. Below is the current working apex trigger however it only fires when the postal code is entered as a 5 digit code "which matches the custom object Name field of Postal Code. I want users to be able to ender the 9 digit postal codes in the billing postal code field and still be able to have this trigger work in taking the 1st 5 digits to use for the list. Thoughts?
trigger updatepostalcode on Account (before insert, before update) {
List<String> BillingPostalCodes = new List<String>();
for (Account a:Trigger.new){
BillingPostalCodes.add(a.BillingPostalCode);
}
List <Postal_Code__c> PostalCodeList = [Select ID, Name from Postal_Code__c where Name in :BillingPostalCodes];
for (Integer i = 0; i <Trigger.new.size(); i++){
if (PostalCodeList.size() > 0 && Trigger.new[i].BillingPostalCode !=null){
for (Postal_Code__c z:PostalCodeList){
if (Trigger.new[i].BillingPostalCode == z.name){
Trigger.new[i].Postal_Code__c = z.ID;
}
}
}
else{
Trigger.new[i].Postal_Code__c = null;
}
}
}
trigger updatepostalcode on Account (before insert, before update) {
List<String> BillingPostalCodes = new List<String>();
for (Account a:Trigger.new){
BillingPostalCodes.add(a.BillingPostalCode);
}
List <Postal_Code__c> PostalCodeList = [Select ID, Name from Postal_Code__c where Name in :BillingPostalCodes];
for (Integer i = 0; i <Trigger.new.size(); i++){
if (PostalCodeList.size() > 0 && Trigger.new[i].BillingPostalCode !=null){
for (Postal_Code__c z:PostalCodeList){
if (Trigger.new[i].BillingPostalCode == z.name){
Trigger.new[i].Postal_Code__c = z.ID;
}
}
}
else{
Trigger.new[i].Postal_Code__c = null;
}
}
}