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
Jenna HildebrandJenna Hildebrand 

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:
// 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!
Paul S.Paul S.
Jenna - in the cases where it didn't work, you've confirmed that the BillingPostalCode wasn't something funky that would have caused it to not resolve to a usable zip code?  For example, a leading zero dropping from a zip code ("1234" instead of "01234") such that the trigger is trying to match "1234."
Jenna HildebrandJenna Hildebrand
Hi Paul,

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!
Paul S.Paul S.
Strange indeed.  Just to try something different, try replacing line 8 with this and see if anything changes.
zipCodeToLookup = zipCodeFull.replaceAll('[^0-9]','').left(5);

 
Jenna HildebrandJenna Hildebrand
I appreciate the suggestion. Unfortunately, it doesn't seem to be working. Is that logic supposed to replace the digits with "text"? Do you think accounting for the dash in the regex could make a difference?

I really appreciate your help!
Paul S.Paul S.
It would essentially be removing any non-numeric characters.  Should note that there's no space after the regex, it's just two apostrophies.  So 75024-2139 becomes 750242139.  I though perhaps there was some "character" sneaking it's way in before the text that was throwing things off.  Admittedly, it was a long shot.

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.)
// for new accounts being created...
for (Account a :account) {

    String zipCodeFull = a.BillingPostalCode;
    String zipCodeToLookup;

    if (zipCodeFull != null && zipCodeFull.length() > 5) {
        System.debug('Initial zipCodeFull: ' + zipCodeFull + ' is ' + zipCodeFull.length() + ' characters');
        zipCodeToLookup = zipCodeFull.left(5);
        System.debug('zipCodeToLookup is now: ' + zipCodeToLookup;
    }
    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';
    }
}

 
Jenna HildebrandJenna Hildebrand
*seven months later* *facepalm*

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!