You need to sign in to do that
Don't have an account?

Help with Using Wildcard Logic in Apex
Hello,
I created an apex class and trigger to handle my org's territory management. It works by searching for the zip code of a new or updated account in our custom Zip Code object. Zip Code records have a look-up field to another custom object called Territory. If the zip code is found, it populates an account field with the "owner" of that Zip Code's Territory.
This has worked swimmingly, except for when zip codes are the full format (00000-0000). Sometimes the Territory is properly assigned in these cases, and sometimes it isn't—even though we've accounted for the variance in zip code format in the below code:
Thank you!
I created an apex class and trigger to handle my org's territory management. It works by searching for the zip code of a new or updated account in our custom Zip Code object. Zip Code records have a look-up field to another custom object called Territory. If the zip code is found, it populates an account field with the "owner" of that Zip Code's Territory.
This has worked swimmingly, except for when zip codes are the full format (00000-0000). Sometimes the Territory is properly assigned in these cases, and sometimes it isn't—even though we've accounted for the variance in zip code format in the below code:
// for new accounts being created... for (Account a :account) { String zipCodeFull = a.BillingPostalCode; String zipCodeToLookup; if (zipCodeFull != null && zipCodeFull.length() > 5) { zipCodeToLookup = zipCodeFull.left(5); } else { zipCodeToLookup = zipCodeFull; } // if the zip code map contains the account zip code... if(zipVsTerritoryMap.containsKey(zipCodeToLookup)) { // then assign the appropriate territory id a.Account_Territory__c = zipVsTerritoryMap.get(zipCodeToLookup); } else { a.Account_Territory__c = 'a220f0000021ihA'; } }Would a better option than using the left() class be to somehow use wildcard * logic? If so, I'd greatly appreciate suggestions on how to implement this. I'm also open to any other ways to make this assignment work better for full-formated zip codes as well as five-digit ones.
Thank you!
I thought leading zeros could be the culprit, but I just looked for some discrepancies and found an example that says otherwise. One account with a zip code of "75024-2139" was properly assigned while another account with a zip code of "75024-2188" was not. Even after changing the -2188 zip code that didn't assign to the -2139 that did, the territory was not assigned. However, when I remove the dash and last four digits, it assigns properly. So strange!
I really appreciate your help!
Have you tried adding debug statements to the code? You could debug your different zip code variable to see what was being returned by each line. (You could add other statements as needed/desired.)
I ended up running out of bandwidth to keeping messing with the apex class and trigger, but for anyone who may be dealing with a similar scenario, I wanted to share the workaround that has fixed my problem. Since the apex trigger managing our territory assignments only works without failure on 5-digit zip codes, I created a workflow rule with a field update that only keeps the left five digits of the zip code (if those left five digits are valid characters). This workflow runs every time an account is updated. So far, so good.
I appreciate your willingness to help, Paul S!