I have this class that call a Queueble job more than 50 times in a loop. I recive the error: Too many queueable jobs added to the queue. The limit is 50.
Here is the class:
global class NSStatusOrderCronSC implements Schedulable {
global void execute(SchedulableContext sc)
{
String norder;
String IdOpp;
List<Opportunity> opList = [SELECT Id FROM Opportunity Where StageName != 'Cerrado Ganado' AND StageName != 'Cancelado'];
List<Order> acList = [SELECT Pedido__c, Oportunidad__c FROM Order WHERE Pedido__c != null AND Oportunidad__c in :opList];
for(Order ac : acList)
{
IdOpp = ac.Oportunidad__c;
norder = ac.Pedido__c;
if(!acList.isEmpty()){
ID jobID = System.enqueueJob(new NSQueuebleJob(IdOpp, norder));
}
}
}
}
Inside the NSQueuebleJob i have this:
public class NSQueuebleJob implements Queueable, Database.AllowsCallouts {
private String IdOpp;
private String norder;
public NSQueuebleJob(String IdOpp, String norder){
this.IdOpp = IdOpp;
this.norder = norder;
}
public void execute(QueueableContext context) {
String endpoint = 'http://url;
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('GET');
req.setHeader('Content-Type', 'application/json');
//LOGIC to update some fields
}
}
How can i search all the opportunities that have created orders and update some fields without reaching the limit?