1

I have a method in beforeInsert trigger, when new SObject is inserted it should add a numeric prefix. That alone is working, but my assignment now is make it much shorter. Currently I'm having 1 block of code for each sobject.

public static  void addPhonePrefix(List<SObject> sObjectsList){
            if (sObjectsList.get(0)instanceof Account) {
                for (SObject sobj : sObjectsList) {
                    Account account  = (Account)sobj;
                    //e.g. account.Phone = '+421'+account.Phone;
                    //CONDITIONS ... }
            }
            if (sObjectsList.get(0)instanceof Contact) {
                for (SObject sobj : sObjectsList) {
                    Contact contact  = (Contact)sobj;
                    //CONDITIONS ... }
            }
            if (sObjectsList.get(0)instanceof Lead) {
                for (SObject sobj : sObjectsList) {
                    Lead lead  = (Lead)sobj;
                    //CONDITIONS ... }
            }
}

Is there a way to use 1 block of code (conditions) for 3 sobjects (Leads, Contacts, Accounts) at once?

1 Answer 1

3

This is where Dynamic Apex shines. You can write code just like this:

Map<sObjectType, sObjectField[]> fieldMap = new Map<sObjectType, sObjectField[]> {
    Account.sObjectType => new List<sObjectField> { Account.Phone },
    Contact.sObjectType => new List<sObjectField> { Contact.Phone },
    Lead.sObjectType => new List<sObjectField> { Lead.Phone }
};
public static void addPhonePrefix(sObject[] records) {
    for(sObject record: records) {
        for(sObjectField field: fieldMap.get(record.getSobjectType())) {
            String phone = (String)record.get(field);
            // conditions
        }
    }
}

The first part describes the fields you want to handle, and the method itself just uses those dynamic values to process data. You could further add a describe method that finds phone fields dynamically, but this would have serious CPU implications. This code is probably close to the optimal version you could ask for, a fair balance between CPU time and code size.

P.S. You could also have used switch statements to avoid the extra condition checks:

for(sObject record: sObjectsList) {
  switch on (record) {
    when Account accountRecord {
      String phone = accountRecord.Phone;
      // conditions
    }
    when Contact contactRecord {
      String phone = contactRecord.Phone;
      // conditions
    }
    when Lead leadRecord {
      String phone = contactRecord.Phone;
      // conditions
    }
  }
}

Though this is still longer than Dynamic Apex in most cases.

2
  • Its working on 'phone' but not on the actual 'record.get(field)'.... How can I cast that updated phone number back into the field?
    – jur1s77
    Oct 13, 2022 at 10:02
  • 1
    @jur1s77 Use record.put(field, newValue).
    – sfdcfox
    Oct 13, 2022 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.