1

I have a trigger on the Account object and custom setting. I am comparing the account object's "Account Potential" field with the custom setting's "slab Maximum" and "Slab Minimum" filed and according to that update Account's "category" field.

The issue we are facing here is that when these slabs got updated. Then for an existing account's categories should update by themselves but unfortunately, it is not happening. Please suggest the way to achieve this. Any help will be appreciated.

Below is my handler class of trigger:

public class CalculateAccountCategory{

    public static boolean isExecuted = false;

    public static void calAccCategory(List<Account> lstAcc){

        if(isExecuted == false){
            Set<Id> accIds = new Set<Id>();
            for(Account a:lstAcc){
                accIds.add(a.Id);
            }

            List<Account> queryAccount = [Select Id, Account_Potential__c, Calculated_Account_Category__c, Vertical__c From Account WHERE Id IN: accIds AND Overwrite_Category__c = False];
            List<Account_Category_Slabs__c> accSlabs = Account_Category_Slabs__c.getall().values();
            Map<Id, String> accToUpdate = new Map<Id, String>();
            List<Account> accToUpdateActual = new List<Account>();

            for(Account_Category_Slabs__c acs: accSlabs){
                for(Account a: queryAccount){
                    if(a.Vertical__c == acs.Vertical__c){
                    if(acs.Slab_Maximum__c != null){

                    if(a.Account_Potential__c <= acs.Slab_Maximum__c && a.Account_Potential__c >= acs.Slab_Minimum__c){
                    accToUpdate.put(a.Id,acs.Category__c);
                    }
                    }else if(acs.Slab_Minimum__c != null){


                    if(a.Account_Potential__c >= acs.Slab_Minimum__c){
                    accToUpdate.put(a.Id,acs.Category__c);
                    }
                    }
                    }
                }
            }

            if (!accToUpdate.isEmpty()){
            isExecuted = true;
            for(Id aId:accToUpdate.keySet()){
                Account acc = new Account(Id=aId,Calculated_Account_Category__c=accToUpdate.get(aId));
                accToUpdateActual.add(acc);
            }   
            update accToUpdateActual;
            }
        }
    }
}
1
  • 1
    It would be normal to add a trigger on the object holding data that drives the behaviour of other objects and have that initiate something like a batch to go and re-evaluate and update those other objects. However, you cannot write triggers for custom settings or custom metadata types. So you need to do something like periodically look for changes. Ah, I see cropredy just suggested that :D
    – Phil W
    Jul 9 '20 at 17:27
3

When a custom setting value changes, and existing records need to be updated because those custom settings change, you need to:

  • Run a scheduled job on Account that invokes a batchable
  • That examines the current values of the custom setting
  • And updates the Account(s) as necessary

The scheduled job would examine the custom settings and if their LastModifiedDate had not changed since the last time the scheduled job ran, the schedulable would simply abort itself (i.e. do nothing)

There is no way to run triggers when a custom setting changes.

Idea to vote up: Triggers and Validation Rules on Custom Settings

1
  • Thank you so much, Cropredy! I will implement this.
    – jsmith
    Jul 9 '20 at 17:30

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.