1

I wrote my first Queueable class

  public class ExpensePDF_Async_Helper implements Queueable, Database.AllowsCallouts{

                private List<Expense_report__c> PDFsTocreate;
                private Integer allowedCallouts;

                public ExpensePDF_Async_Helper(set<id> approvedIdset){

                Integer allowedCallouts = Limits.getLimitCallouts() - Limits.getCallouts();            
                PDFsTocreate = [select PDF_created__c from expense_report__c where id in: approvedIdset LIMIT :allowedCallouts];
                }

                public void execute(QueueableContext qc)
                {
                   if(allowedCallouts <=0)
                   return;

                   if(PDFsToCreate.isEmpty())
                   return;

                   for(Expense_report__c repo : PDFsTocreate)
                   {
                       if(repo.PDF_created__c = false){
                       //Callouts for getContentasPDF
                       ExpenseReportPDF.savePDFattachment(repo.id);
                       repo.PDF_Created__c = true;             
                       }
                   }
                    update PDFsTocreate;
                }
  }

Tried it invoking anonymously

set<id> setid = new set<id>();
setid.add([select id from expense_report__c where id='a0Nq00000027YWg'].id);
ID jobID = system.enqueueJob(new ExpensePDF_Async_Helper(setid));
system.debug(jobID);
AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID];
system.debug(jobInfo.status);

I see that only the constructor is getting called and execute remains uncalled.

  • 2
    The execute method is going to be in a separate log... did you make sure you checked for all logs? – sfdcfox Mar 4 '16 at 18:34
3

Your code is executing-- it comes in a separate log. However, your execute method has at least one logic bug which is preventing it from having the desired behavior.

The main problem is here:

if(repo.PDF_created__c = false){

In Apex Code, = is an assignment operator, and == is comparison. Since you used =, you're assigning the value false to PDF_Created__c, which in turn causes the condition to be false, so the inner code doesn't execute.

Your code has other problems as well, but this is the main issue you've got that prevents your code from working correctly.

|improve this answer|||||
  • Thanks Brian, that was farcical from me. Can you please advise what other problems do you see apart from not querying PDF_created__c in the SOQL? – Jarvis Mar 4 '16 at 20:14
  • Are you referring to the non-existent null checks ? – Jarvis Mar 4 '16 at 20:23
  • 1
    @Bennie You have a number of issues I can see. First, it doesn't matter if PDFStoCreate is empty; you could just iterate over the list and do your thing; if there's no records to use, nothing happens. Secondly, you're checking for your allowedCallouts in one context, but then jumping to a different context to check. While I don't think this is harmful today, it could come back to bite you later. Third, you may as well query PdfsToCreate inside the execute method, because asynchronous code might run a while after the original context, and those records might be modified/deleted inbetween. – sfdcfox Mar 4 '16 at 20:48
2

I think execute method is defiantly getting called. Create one test record or do some DML as first action in execute method.

For ex.

public void execute(QueueableContext qc)
{
    Lead l = new lead(LastName='TEST', Company='My Company');
   Insert l;
   if(allowedCallouts <=0)
   return;

   if(PDFsToCreate.isEmpty())
   return;

   for(Expense_report__c repo : PDFsTocreate)
   {
       if(repo.PDF_created__c = false){
       //Callouts for getContentasPDF
       ExpenseReportPDF.savePDFattachment(repo.id);
       repo.PDF_Created__c = true;             
       }
   }
    update PDFsTocreate;
}

And then query the record to check if our lead is there

Select id, LastName from lead where lastName='TEST'

The other thing that you should check is run below query and see if your job status

AsyncApexJob jobInfo = [SELECT Status,NumberOfErrors FROM AsyncApexJob WHERE Id=:jobID]

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

|improve this answer|||||
  • Defiantly? Well I won't disagree then!! – Caspar Harmer Mar 4 '16 at 21:03
  • sarcasm? if you notice sfdcfox and me gave answer at same time – AtulRajguru9 Mar 5 '16 at 10:20
  • Sorry that was naughty of me – Caspar Harmer Mar 5 '16 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.