Skip to content
Bob Hatcher edited this page Mar 11, 2021 · 2 revisions

Using this pattern, you can easily create a Queueable that is invoked very easily.

The idea is that your code will maintain a few things per object:

  • ObjectTrigger
  • ObjectTriggerHandler (Synchronous)
  • ObjectTriggerHandlerQueueable (Queueable)

And the Queueable will handle anything that does not need to happen synchronously in the transaction.

The MyQueueable Abstract Class

This includes an abstract class, MyQueueable. It maintains a Map<Id,List<sObject>> where the String is an "instruction" and the list is the list of objects to work on. for example, if you wanted to set the Name of a record, "Instruction" might be "SetName" and the List is a list of records.

Your Extension

To Extend MyQueueable you should create a class patterned on the example AccountTriggerHandlerQueueable. It should contain:

  • Public static final String fields for your instructions.
  • A public static final String ClassName - this is helpful for debugging.
  • An override method for execute().
  • Recommended, but not required, a constructor that takes a String and a List and calls addInstruction(String,List) for you (public MyConstructor(String instruction, List<sObject> records) {})
  • The execute(QueueableContext) and fire() methods, which are boilerplate.

Usage

These utilities make "peeling off" a Queueable very easy. You would simply need to instantiate the Queueable, add instruction(s), and fire it.

AccountTriggerHandlerQueueable atq = new AccountTriggerHandlerQueueable();

atq.addInstruction(atq.INSTRUCTION_REPARENT, reparentedAccounts);

atq.fire();

This package also includes some other items such as test code and error handling which is left over from my actual implementation, you can retain or scrap that.

There's Lots Of Code Here. What do I Really Need?

If you want to get straight to the point all you need is the MyQueueable class, then implement an extension according to the pattern indicated above. The other code is provided in the interest of a complete example.

Clone this wiki locally