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
Raj R.Raj R. 

How to call queueable class from trigger?

Hi,

We have a need to call a queueable apex class from a trigger after insert. Would it be possible to do the following below as we need to also do chaining? We need to perform updates to different objects in consecutive order so wanted to use a queue. 
trigger SomeTrigger on CustomObject__c (after insert) {
       System.enqueuejob(new  QueueClass1(Trigger.new))
}

public class QueueClass1 implements Queueable {
              private List<CustomOBject__c> objs;
              
              public QueueClass1(List<CustomObject> recs) {
                            this.objs = recs;
              }
              
               public void execute(QueueableContext queCont) {
                            //execute some logic on objs list
                            //perform update

                          //call second queueable apex job
                         System.enqueueJob(new QueueClass2(recs));
               }

}

public class QueueClass2 implements Queueable {
              private List<CustomOBject__c> objs;
              
              public QueueClass2(List<CustomObject> recs) {
                            this.objs = recs;
              }
              
               public void execute(QueueableContext queCont) {
                            //execute other logic on objs list
                            //create records for another objects
                            List<Account> acnts; //acnts list is populated based on logic executed in the "other logic"
                             insert acnts;
               }

}

 
Best Answer chosen by Raj R.
@GM@GM
Hi,

are you getting any errors? I just called Queueable job from trigger & its working as expected but not sure about chaining.

trigger LeadTrigger on Lead(before insert) {
if(Trigger.isBefore){
if(Trigger.isInsert){
System.enqueueJob(new QueueableExample(Trigger.New));
}
}
}

All Answers

@GM@GM
Hi,

are you getting any errors? I just called Queueable job from trigger & its working as expected but not sure about chaining.

trigger LeadTrigger on Lead(before insert) {
if(Trigger.isBefore){
if(Trigger.isInsert){
System.enqueueJob(new QueueableExample(Trigger.New));
}
}
}
This was selected as the best answer
Gangadhar LGangadhar L
Hi @Raj R and @GM,
If you provide test class for this code. it will be great helpful for me. I'm facing some problem in test class for this code.
Er. Prashant Veer SinghEr. Prashant Veer Singh
Hi @Gangadhar L,

I am too late to answer but with a taught that, this may help others, I am posting the answer to your solution.

Writing test class is simple. But they care of following things:
1. You have to write test class for both queueable class seprately.
2. After creating data of a class, in between test.startTest() and test.StopTest() call your class e.g. system.enqueueJob(new QueueClass1 ());

You get all uselful videos:
https://www.youtube.com/channel/UCtmcyCNzb4g_RfhM85iS2Hw

Please, hit like button and if you need more explaination/help email me at globalsfdcprofessionals18@gmail.com

thanks