2

I would like to know what is the most efficient way to loop through all fields in trigger a trigger so I can check against the field name for additional actions. I'm not sure how I can do this and would appreciate some advise.

Here is some pseudo code to help you understand what I'm trying to do:

// go through list of accounts
for(Account acc: Trigger.new) {

    // go through list of fields in Accounts
    // this is the section I'm not sure what the best way to go through fields
    for(FIELD : acc) { // <- not sure what to loop through

        // my logic for checking field against other logic

    }

}

Updated Code:

public static void matchedRecords(Map<Id, Account> triggerMapOld, Map<Id, Account> triggerMapNew){

    List<MyCustomSettingMonitor__c> customSetting = [SELECT Id, Field__c FROM MyCustomSettingMonitor__c];

    // go through each account record
    for(Account acc: triggerMapNew.values()) {

        for(MyCustomSettingMonitor__c customField : customSetting) {

            Object accountField = acc.get(customField.Field__c);
            system.debug('customField.Field__c: ' + accountField);

            // check if specified field has its value changed against Trigger.Old
            system.debug('old value:' + triggerMapOld.get(acc.Id).get(accountField));


        }

    }

}
4
  • Are you looking at iterating through all the fileds of a given object? Schema describe maybe? salesforce.stackexchange.com/questions/74275/… Oct 24, 2017 at 9:13
  • 3
    I would suggest to loop through the custom settings and then using the values there to get the account fields. That probably means less iterations.
    – Folkert
    Oct 24, 2017 at 9:14
  • Or if you have the field name, then I would recommend using like acc[fieldnameString]. You can get the values directly. Oct 24, 2017 at 9:16
  • Be sure to use customField.Field__c to get the value from the oldMap instead of accountField
    – Folkert
    Oct 24, 2017 at 10:16

1 Answer 1

3

It's probably better to iterate through the custom settings instead of the account fields, as there are probably less of them thus saving you iterations and making it more efficient.

Try something like this:

List<FieldSettings__c> settings = FieldSettings__c.getall().values();

for(Account acc: Trigger.new) {

    for(FieldSettings__c setting : settings) { 
        if (acc.get(setting.Field_Name__c) != Trigger.oldMap.get(acc.Id).get(setting.Field_Name__c)) {
            Object accountFieldvalue = acc.get(setting.Field_Name__c);
            // do what you need to do with the value
        }
    }

}
3
  • I get two problems with this approach, accountFieldvalue appears to be null in my testing. Lastly how can I use .get() if I'm also fetching values from another map (i.e. checking to see if the trigger.new value has changed from trigger.old at the same time as checking if the field exists in my custom setting) Oct 24, 2017 at 9:58
  • What is the value that you use for the field name? It must be the developer name (so including __c if it's a custom field). I'll add some code to illustrate how to check if the new and old values changed. You need to use the trigger maps instead of lists.
    – Folkert
    Oct 24, 2017 at 10:11
  • Sorry update the code, I am using a dynamic value within the for loop to check against fields. My custom setting will hold the API name of the field so it should be a direct match. Oct 24, 2017 at 10:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .