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
Chris Walters 9Chris Walters 9 

Is it possible to create an AddressHandler ?

version 47

We've several objects, both std and custom,  that contain  Address compound fields. I'd like to create an AddressHandler, similar to ContactHandler, to apply various corrections to an Address regardless of which object it is tied to. Is this possible? I can reference System.Address in my Apex but there are no setters to go with the getters (!???!) Or must I put  boilerplate into ContactHandler, AccountHandler, LeadHandler and all the other Handlers for objects with one or more Address compound field?  Surely there's a better way.

 
Chris Walters 9Chris Walters 9
more details...

I can create
public static void standardizeAddress( System.Address addressToFix) {
        System.debug('AddressHandler:standardizeAddress - entering');
        Boolean needToUpdate = False;
        
        if( countryCodeMap.containsKey( addressToFix.Country)) {
//          addressToFix.Country = countryCodeMap.get( addressToFix.Country);   <-- here
            needToUpdate = True;
        }
        if( addressToFix.Country == 'USA') {
            if( stateNameToCodeMap.containsKey( addressToFix.State)) {
//              addressToFix.State = stateNameToCodeMap( addressToFix.State);   <-- here
                needToUpdate = True;
            }
        }
        
        if( needToUpdate) {
//          update addressToFix;
        }
        
        System.debug('AddressHandler:standardizeAddress - exiting');
    }

where my ContactHandler.afterInsert() is calling this method
 
public void afterInsert(SObject so) {
        Contact contact = (Contact) so;
        AddressHandler.standardizeAddress( contact.MailingAddress);
        ...
now here (https://developer.salesforce.com/blogs/developer-relations/2012/05/passing-parameters-by-reference-and-by-value-in-apex.html) it says Apex sObjects are pass-by-reference - great, that's the behavior I want. And here (https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/compound_fields_limitations.htm) it talks about compound-field uses and limitiations where there's an example of setting a specific field value . So this should be doable, I just can't seem to get the syntax right.

If I can't do an update on the compound field , I could change the function to return a boolean  then adjust afterInsert like so
 
public void afterInsert(SObject so) {
        Contact contact = (Contact) so;
        if( AddressHandler.standardizeAddress( contact.MailingAddress)) {
                update contact;
        }
        ...

Any ideas?