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
uma52551.3972270309784705E12uma52551.3972270309784705E12 

Using Substring(0,5) for Zipcode Apex Trigger

Hi All,
 String zipcode1 = a.BillingPostalCode.substring(0,5);
        if(a.BillingPostalCode!=NULL&&countyMap.containsKey(zipcode1)) {
            a.County__c = countyMap.get(zipcode1).County__c;
   }
If I am using the above code it is working for zipcode1 > = 5  but throwing error System.StringException: Ending position out of bounds: for zipcode1<5  i.e; if Zipcode1 = 32809- it is updating the county as Orange
                        if Zipcode1 = 3280 it is throwing the above error on the account page.

Can any one help me how to avoide this error on account page.

Thanks,
Best Answer chosen by uma52551.3972270309784705E12
Shaijan ThomasShaijan Thomas
String zipcode1;
if (a.BillingPostalCode.length() >= 5)
    zipcode1 = a.BillingPostalCode.substring(0,5);
else
    zipcode1 = a.BillingPostalCode.substring(0,4);
Please try this

All Answers

Vijay NagarathinamVijay Nagarathinam
Hi Uma,

Try this and let me know it will work or not.

string zipcode1 = a.BillingPostalCode.substring(0,4);
uma52551.3972270309784705E12uma52551.3972270309784705E12
for substring(0,4) it is not giving error but not updating the county for five and above digits zip code
Shaijan ThomasShaijan Thomas
String zipcode1;
if (a.BillingPostalCode.length() >= 5)
    zipcode1 = a.BillingPostalCode.substring(0,5);
else
    zipcode1 = a.BillingPostalCode.substring(0,4);
Please try this
This was selected as the best answer
uma52551.3972270309784705E12uma52551.3972270309784705E12
Thankyou shaijan thomas Problem solved!