+ Start a Discussion
John AthitakisJohn Athitakis 

Queueable Code Issues

Synopsis: I have a custom objet (Sales_Areas__c) that Opportunities have a lookup toward. Opportunities have lookups and other data that I want updated when the Sales Area is updated (with related info). I have this set as such.

Trigger on Sales Area fires on edit, calls a queueable class that collects all the related opportunities (since there can be very very many) and 'updates' them.
Trigger on Opportunity that fires on edit, calls a queuable class that gathers all the relevent data from its related Sales area and makes changes to the Opportunity.

In this sense, an edit to the Opportunity or Sales area results in an update (desired behavior). I moved from some less effective @future code to queueable because of issues I was facing with @future code and now, have encountered problems with queueable.

My old Opportunity code prevented recussions by using the isFuture(); syntax before calling the old @future code. Since what invoked my Opportunity code at the Sales area was a Queueable, this was not a problem. But now that they are both Queueable, I'm facing this issue:

Too many queueable jobs added to the queue: 2

It seems my Opportnity Code (which edits the Opportunity) is invoking itself. I attempted to prevent this via:

trigger OpportunityReferenceUpdate on Opportunity (before insert, before update) {
    if(Limits.getQueueableJobs()<2){ 
        List<Opportunity> opportunityRecords = new List<Opportunity>();      
        for (Opportunity op : Trigger.new){
        opportunityRecords.add(op);
        }
    OpportunityReferenceQueuable updateJob = new OpportunityReferenceQueuable(opportunityRecords);
    ID jobID=System.enqueueJob(updateJob);
    }

But that does not seem to work? Em I using Limits.getQueueableJobs() incorrectly? 

Is there a way to check the name of the class that invoked the current method via isQueueable() (something other than a boolean, but maybe the name of the job).
 

Thank you. 

 

 

Fergal McMenaminFergal McMenamin
Hi John,

I'm in the same boat - did you manage to resolve this?

F