You need to sign in to do that
Don't have an account?
Josh 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!
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!
Please let me know if that is unclear in anyway.
Thanks!
below is the trigger that you may use
I thank you very much for your help in advance!
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
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.
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
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
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.