0

today i'm facing some issues with the queueable interface.

This is my use case: i gave to send an email with a pdf attachment when an object is created.

This is my solution: When user create a record, a queueable class is instantiated and enqueued in the apex job queue. The queueable class receive the Account as input and than create the PDF and send it by email.

This is my problem: the generated job stuck in the queue for hours until a System Error is returned.

This is my code: page to render as pdf

<apex:page showHeader="true" sidebar="true" controller="TestQueueAndPDFCtrl" renderAs="{!renderAs}">    
    <apex:outputText value="test content {!accid}" />
</apex:page>

page controller

public with sharing class TestQueueAndPDFCtrl {

    public String accid {get;set;}
    public String renderAs {get;set;}

    public TestQueueAndPDFCtrl() {
        // get account id from parameter and set on the page
        accid = ApexPages.currentPage().getParameters().get('accId');
        renderAs = ApexPages.currentPage().getParameters().get('renderAs');
    }

    public static void sendPdf() {

        /* create an account and then send the id by email */

        Account a = new Account();
        a.Name = 'marco';
        insert a;

        Id job = System.enqueueJob(new TestQueueableWithCallout(a));
    }
}

queueable class

public class TestQueueableWithCallout implements Queueable, Database.AllowsCallouts {

    private Account a;

    public TestQueueableWithCallout(Account a) {
        this.a = a;
    }

    public void execute(QueueableContext context) {

        System.debug('queueble start');

        PageReference pdf = Page.TestQueueAndPDF;
        pdf.getParameters().put('accId', a.id);
        pdf.getParameters().put('renderAs', 'PDF');

        Blob b = pdf.getContentAsPDF();
        // Blob b = Blob.valueOf('ciao');

        Messaging.EmailFileAttachment att = new Messaging.EmailFileAttachment();
        att.setFileName('ciao.pdf');
        att.setbody(b);

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new List<String>{'m.*****@******.it'});
        mail.setsubject('test');
        mail.setFileAttachments(new List<Messaging.EmailFileAttachment>{att});
        mail.sethtmlbody('body');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

        System.debug('queueable done');
    }
}

If i execute this from anonymous

    TestQueueAndPDFCtrl.sendPdf();

I get the following debug logs: anonymous log:

34.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
Execute Anonymous:     TestQueueAndPDFCtrl.sendPdf();
12:09:36.053 (53908105)|EXECUTION_STARTED
12:09:36.053 (53923354)|CODE_UNIT_STARTED|[EXTERNAL]|execute_anonymous_apex
12:09:36.054 (54371890)|SYSTEM_MODE_ENTER|false
12:09:36.055 (55896071)|METHOD_ENTRY|[1]|01p2400000BKIr7|TestQueueAndPDFCtrl.TestQueueAndPDFCtrl()
12:09:36.055 (55917750)|METHOD_EXIT|[1]|TestQueueAndPDFCtrl
12:09:36.055 (55976593)|METHOD_ENTRY|[1]|01p2400000BKIr7|TestQueueAndPDFCtrl.sendPdf()
12:09:36.056 (56268339)|DML_BEGIN|[15]|Op:Insert|Type:AsyncRequest__c|Rows:1
12:09:36.106 (106851209)|DML_END|[15]
12:09:36.107 (107060659)|METHOD_ENTRY|[1]|01p2400000BKIrH|TestQueueableWithCallout.TestQueueableWithCallout()
12:09:36.107 (107076652)|METHOD_EXIT|[1]|TestQueueableWithCallout
12:09:36.107 (107116188)|CONSTRUCTOR_ENTRY|[17]|01p2400000BKIrH|<init>(AsyncRequest__c)
12:09:36.107 (107203431)|CONSTRUCTOR_EXIT|[17]|01p2400000BKIrH|<init>(AsyncRequest__c)
12:09:36.107 (107234344)|SYSTEM_METHOD_ENTRY|[17]|System.enqueueJob(APEX_OBJECT)
12:09:36.131 (131073893)|SYSTEM_METHOD_EXIT|[17]|System.enqueueJob(APEX_OBJECT)
12:09:36.131 (131111004)|METHOD_EXIT|[1]|01p2400000BKIr7|TestQueueAndPDFCtrl.sendPdf()
12:09:36.131 (131122639)|SYSTEM_MODE_EXIT|false
12:09:36.135 (135270153)|CUMULATIVE_LIMIT_USAGE
12:09:36.135 (135270153)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 0 out of 100
  Number of query rows: 0 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 1 out of 150
  Number of DML rows: 1 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 1 out of 50
  Number of Mobile Apex push calls: 0 out of 10

12:09:36.135 (135270153)|CUMULATIVE_LIMIT_USAGE_END

12:09:36.135 (135325267)|CODE_UNIT_FINISHED|execute_anonymous_apex
12:09:36.137 (137826095)|EXECUTION_FINISHED

queueable log

31.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
18:02:29.642 (8319169642555656)|EXECUTION_STARTED
18:02:29.642 (8319169642588553)|CODE_UNIT_STARTED|[EXTERNAL]|01p2400000BKIrH|TestQueueableWithCallout
18:02:29.649 (8319169649540178)|METHOD_ENTRY|[1]|01p2400000BKIrH|TestQueueableWithCallout.TestQueueableWithCallout()
18:02:29.649 (8319169649556220)|METHOD_EXIT|[1]|TestQueueableWithCallout
18:02:29.649 (8319169649633576)|SYSTEM_METHOD_ENTRY|[11]|System.debug(ANY)
18:02:29.649 (8319169649669261)|USER_DEBUG|[11]|DEBUG|queueble start
18:02:29.649 (8319169649677255)|SYSTEM_METHOD_EXIT|[11]|System.debug(ANY)
18:02:29.649 (8319169649753262)|SYSTEM_METHOD_ENTRY|[14]|System.PageReference.getParameters()
18:02:29.649 (8319169649820068)|SYSTEM_METHOD_EXIT|[14]|System.PageReference.getParameters()
18:02:29.650 (8319169650800024)|SYSTEM_METHOD_ENTRY|[14]|Map<String,String>.put(Object, Object)
18:02:29.650 (8319169650839977)|SYSTEM_METHOD_EXIT|[14]|Map<String,String>.put(Object, Object)
18:02:29.650 (8319169650857858)|SYSTEM_METHOD_ENTRY|[15]|System.PageReference.getParameters()
18:02:29.650 (8319169650892121)|SYSTEM_METHOD_EXIT|[15]|System.PageReference.getParameters()
18:02:29.650 (8319169650917349)|SYSTEM_METHOD_ENTRY|[15]|Map<String,String>.put(Object, Object)
18:02:29.650 (8319169650933187)|SYSTEM_METHOD_EXIT|[15]|Map<String,String>.put(Object, Object)
18:02:29.650 (8319169650947514)|SYSTEM_METHOD_ENTRY|[17]|System.PageReference.getContentAsPDF()
18:02:29.651 (8319169651263827)|CODE_UNIT_FINISHED|TestQueueableWithCallout
18:02:29.652 (8319169652843169)|EXECUTION_FINISHED

It's real strange it's seems that the queued job stops at getContentAsPDF.

Could you help me with this?

Thks ;)

0

I opened a Case to Salesforce and the answer was this: Callouts are NOT allowed in Queueable class right now (Summer15).

It's a work in progress for Winter16

| improve this answer | |

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.