Activity Forums Salesforce® Discussions Chained queuable jobs invoked through trigger.

  • Chained queuable jobs invoked through trigger.

    Posted by inbox on May 17, 2021 at 2:16 pm

    System.AsyncException: Maximum stack depth has been reached.
    Class.Qapex.execute: line 14, column 1

    I am new to salesforce. I want to know how to write a test class for the below code. I have chained 1 child job.

    inbox replied 2 years, 3 months ago 2 Members · 2 Replies
  • 2 Replies
  • Prafull

    Member
    May 30, 2021 at 6:00 am

    As Salesforce document You can’t chain queueable jobs in an Apex test. Doing so results in an error. To avoid getting an error, you can check if Apex is running in test context by calling Test.isRunningTest() before chaining jobs.
    You can refer below link
    https://salesforce.stackexchange.com/questions/181233/system-asyncexception-maximum-stack-depth-has-been-reached
    Now in your code change List l/List LA to List<Account> l, List<Account> LA. There are 2 cases in which you will get error.
    1. Recursive trigger error i.e Too many queueable jobs added to the queue: 2
    2. For chaining the Queueable Job in Test class: System.AsyncException: Maximum stack depth has been reached.
    Please Use below code to resolve your issue
    Parent Queueable
    public with sharing class Qapex implements Queueable{
    private list<Account> L;
    public Qapex(List<Account> a) {
    this.L= a;
    }
    public void execute(QueueableContext q){
    list<Account> LA= new list<Account>();
    for(account f: this.L){
    account acc= new account();
    acc.Name= 'queable name' + f.Name;
    LA.add(acc);
    }
    INSERT LA;
    if(!Test.isRunningTest()){
    system.enqueueJob(new Qapex1());
    }
    }
    }
    Child Queueable
    public with sharing class Qapex1 implements Queueable{
    public void execute(QueueableContext QC){
    account ac = new account(name= 'new name');
    insert ac;
    }
    }
    Trigger
    if(Trigger.isBefore)
    {
    if(Trigger.isInsert || Trigger.isUpdate)
    {
    if(RecursiveTriggerHandler.isFirstTimeBeforeIns){
    system.enqueueJob(new Qapex(trigger.new));
    RecursiveTriggerHandler.isFirstTimeBeforeIns=false;
    }
    }
    }
    Test Classe for Qapex
    @istest
    public class QapexTCls {
    @istest
    public static void Qapex1TClsM() {
    account a= new account(name= 'qapextestname');
    test.startTest();
    insert a; //this will call trigger that is used to invoke queueable class
    test.stopTest();
    account ac= [select id, name from account where id= : a.id];
    system.assertEquals('qapextestname', a.name);
    }
    }
    Test Class for Qapex1
    @istest
    public class Qapex1TCls {
    @istest
    public static void Qapex1TClsM() {
    test.startTest();
    system.enqueueJob(new Qapex1());
    test.stopTest();
    }
    }

  • inbox

    Member
    December 28, 2021 at 11:46 am

    Thank you.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos