+ Start a Discussion
Marco ZeuliMarco Zeuli 

Queueable interface and getContentAsPDF

Hi all,

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>

this is the 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));
	}
}

It has also a static method to enqueue the job.

this is the 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 started!!!');

		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('done');
	}
}

In my ORD i enabled the critical update from SUmmer 15, so the getContentAsPDF behave as a callout.

Could you help me with this?
Anirudh SinghAnirudh Singh
Hi Marco,

What is the error? Can you please provide the whole error message?

Thanks and Regards,
Anirudh Singh
Marco ZeuliMarco Zeuli
Hi Anirudh,

this is the error i get in the Apex Job queue
Salesforce System Error: 1248989408-9559 (115151481) (115151481)

If i run this from anonymous console
TestQueueAndPDFCtrl.sendPdf();

I get these debug logs:

anonymous:
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
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 executed!!!
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 seems that the queueable class stops at the getcontentaspdf ....