0

I have the following trigger:

trigger CaseMainTrigger_tgr on Case (before update, after insert, after update, after delete) 
{
    if(Trigger.isBefore){
        if(Trigger.isUpdate){
       CaseMethods_cls.manageUpdatedCases(Trigger.oldMap, Trigger.newMap);
       CaseMethods_cls.createCaseComments(Trigger.oldMap, Trigger.newMap);
       CaseMethods_cls.updateDate(Trigger.oldMap, Trigger.newMap);

        Map<Id,Schema.Case> mapOldCases = Trigger.oldMap;
        Map<Id,Schema.Case> mapNewCases = Trigger.newMap;
        for(Id iterator:mapOldCases.keySet()){

            if(mapOldCases.get(iterator).ComplainType__c == 'Reportes' && (mapOldCases.get(iterator).OtherComplainceStatus__c != mapNewCases.get(iterator).OtherComplainceStatus__c)){
              SMS smsAsync = new SMS();
                      smsAsync.old = Trigger.oldMap;
                      smsAsync.news = Trigger.newMap;
              System.enqueueJob(smsAsync);  
            } else {
                break;
            }
        }
    }
}

}

And a process like this:

public class SMS implements Queueable, Database.AllowsCallouts{
public  Map<Id,Schema.Case> old {get;set;}
public  Map<Id,Schema.Case> news {get;set;}


public void execute(QueueableContext context) {        
    if(!Test.isRunningTest()){ 
        ValidateActivate.sendSMSNotification_Queja(old,news);
    }        
}    

}

I understand that the error is due to "too many jobs in queue" and that it is a transaction error. The problem is how can I correct my code to skip or replace the process.

Renato Oliveira
  • 13,257
  • 10
  • 60
  • 135

1 Answers1

0

You can add up to 50 jobs to the queue with System.enqueueJob in a single transaction. In asynchronous transactions (for example, from a batch Apex job), you can add only one job to the queue with System.enqueueJob. To check how many queueable jobs have been added in one transaction, call Limits.getQueueableJobs().

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_queueing_jobs.htm

To avoid enqueuing duplicate jobs for the same case, you can try using a set to bulkify your code in this scenario as follows. Further if the problem persists, you can consider frameworks like https://gist.github.com/tshevchuk/c6568601912843c50f97d965d4ce09e2

trigger CaseMainTrigger_tgr on Case (before update, after insert, after update, after delete) {
if (Trigger.isBefore) {
    if (Trigger.isUpdate) {
        CaseMethods_cls.manageUpdatedCases(Trigger.oldMap, Trigger.newMap);
        CaseMethods_cls.createCaseComments(Trigger.oldMap, Trigger.newMap);
        CaseMethods_cls.updateDate(Trigger.oldMap, Trigger.newMap);
    Set<Id> caseIdsToSendSMS = new Set<Id>();
    for (Case updatedCase : Trigger.new) {
        Case oldCase = Trigger.oldMap.get(updatedCase.Id);
        if (oldCase.ComplainType__c == 'Reportes' && oldCase.OtherComplainceStatus__c != updatedCase.OtherComplainceStatus__c) {
            caseIdsToSendSMS.add(updatedCase.Id);
        }
    }

    if (!caseIdsToSendSMS.isEmpty()) {
        List<SMS> smsJobs = new List<SMS>();
        for (Id caseId : caseIdsToSendSMS) {
            SMS smsAsync = new SMS();
            smsAsync.old = Trigger.oldMap;
            smsAsync.news = Trigger.newMap;
            smsAsync.caseId = caseId;
            smsJobs.add(smsAsync);
        }
        for (SMS smsJob : smsJobs) {
            System.enqueueJob(smsJob);
        }
    }
}

}

Vijaya Kapadam
  • 306
  • 2
  • 12
  • Thank you very much for the answer, but in the second part of the Queueable, would everything be the same? :) – Desaarollador x Jul 20 '23 at 16:23
  • I replicated the code, and it generates an error in the caseId class, I omitted it, when I run the code I get an error:

    Trigger.CaseMainTrigger_tgr: line 48, column 1
    17:53:19.732 (23732331825)|CUMULATIVE_LIMIT_USAGE
    17:53:19.732 (23732331825)|LIMIT_USAGE_FOR_NS|(default)|

    -- And Code:
    smsAsync.news = Trigger.newMap;
    smsAsync.caseId = caseId; smsJobs.add(smsAsync);
    }
    48 for (SMS smsJob : smsJobs) {
    System.enqueueJob(smsJob);

    – Desaarollador x Jul 21 '23 at 00:07
  • Trigger.CaseMainTrigger_tgr: line 48, column 1 17:53:19.732 (23732331825)|CUMULATIVE_LIMIT_USAGE 17:53:19.732 (23732331825)|LIMIT_US EDAD_PARA_NS|(predeterminado)| – Desaarollador x Jul 21 '23 at 00:55
  • @ Vijaya Kapadam ->line 48---> for (SMS smsJob : smsJobs) ---- pls help.. – Desaarollador x Jul 21 '23 at 15:18
  • The error (23732331825)|CUMULATIVE_LIMIT_USAGE is raised when you've exceeded one of the governor limits in Apex. It's not exactly clear which limit is being exceeded based on the provided information. Also, chaining will not solve the problem if you're hitting a governor limit in a single execution context. I feel you should consider using batch Apex. – Vijaya Kapadam Jul 22 '23 at 01:13
  • Run this apex query in < Workbench.developerforce >
    List<Case> lstCas = new List<Case>();  
    
    for(Case cs: [SELECT  
    ComplainType__c,Id,NumberFolio__c,Origin,OtherComplainceStatus__c FROM Case]){  
    cs.ComplainType__c = 'Aclaraciones';    
    cs.OtherComplainceStatus__c = 'En Proceso';  
    cs.recordTypeId = '01241000001IHP4AAO';  
    cs.OwnerId = '00G8Y000006eWBbUAM';  
    lstCas.add(cs);  
    }  
    
     if(lstCas.size() > 0){
     update lstCas;
     }
    
    – Desaarollador x Jul 24 '23 at 18:09