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
Salesforce BeginnerSalesforce Beginner 

Queueable

Hi All,

We have an extensive trigger on Account. As our trigger is expensive in terms of CPU utilization, we are hitting CPU time limit exception frequently. As part of improving the performance, we are thinking to move few methods which can be run asynchronous and we are moving those methods out of the trigger into Queueable class.

By including debug statements in our trigger, I identified the methods which is taking comparitively more CPU times. Also, to start with, moved a method to queueable following the documentation.


public class AccountQueueable implements Queueable {
        
    private ID parent;
    Private Boolean isBefore;
    Private Boolean isAfter;
    Private Boolean isInsert;
    Private Boolean isUpdate;
    Private Boolean isDelete;
    Private Boolean isUndelete;
    private List<Account> newList;
    private Map<Id, Account> newMap;
    private List<Account> oldList;
    private Map<Id, Account> oldMap;
    
    public AccountQueueable(Boolean isBefore, Boolean isAfter, Boolean isInsert,
                              Boolean isUpdate,Boolean isDelete,Boolean isUndelete,List<Account> newList,
                              Map<Id, Account> newMap,List<Account> oldList,Map<Id, Account> oldMap)
    {
        this.isBefore = isBefore;
        this.isAfter = isAfter;
        this.isInsert = isInsert;
        this.isUpdate = isUpdate;
        this.isDelete = isDelete;
        this.isUndelete = isUndelete;
        this.newList = newList;
        this.newMap = newMap;
        this.oldList = oldList;
        this.oldMap = oldMap;
    }

    public void execute(QueueableContext context) {
          
        UtilityClass.AccountsUpdate(newList, oldMap);
    }  // End of execute method
  
}

And in the trigger I am using the below statements to enqueue the job:

AccountQueueable AccountsAfterUpdateJob = new AccountQueueable(false,true,false,true,false,false,newAccountList,null,null,oldAccountMap);
System.enqueueJob(AccountsAfterUpdateJob);

When I do the update test, I do not see much improvement in the CPU time (it is pretty much same or bit higher). The purpose of moving it to the queueable was to save the CPU time of the trigger during the transaction. Can anybody please let me know if I am doing anything incorrectly here?

Need help and suggestions.

 
Pankaj MehraPankaj Mehra
Hi Mate,

I recommad using future method from trigger, althought there is a limit in firing future methods, you can send a set of records to future methods, say 200 records to one future methods that will cover 200 * 100 records.

Another option is to schedule a batch class

Both the process are asynchronous.

Thanks