You need to sign in to do that
Don't have an account?

Callout loop not allowed error when using getContent and getContentAsPDF
Hi there
Salesforce does not allow you to create PDF attachments in batch job.
This is work around to that.
https://developer.salesforce.com/forums/?id=906F0000000AlGoIAK
It works!
But As of Summer 15, Salesforce have implemented a critical update "PageReference getContent() and getContentAsPDF() Methods Treated as Callouts". Once enabled, you may get the rather uninformative error "(304497466)|FATAL_ERROR|System.CalloutException: Callout loop not allowed".
And so I have the problem described in this blog: http://codrspace.com/gwickman/callout-loop-not-allowed-error-when-using-getcontent-and-getcontentaspdf/
Can someone please help me get around this error or help with another way to create PDF attachments for emails in a batch job?
Thank you very much
Salesforce does not allow you to create PDF attachments in batch job.
This is work around to that.
https://developer.salesforce.com/forums/?id=906F0000000AlGoIAK
It works!
But As of Summer 15, Salesforce have implemented a critical update "PageReference getContent() and getContentAsPDF() Methods Treated as Callouts". Once enabled, you may get the rather uninformative error "(304497466)|FATAL_ERROR|System.CalloutException: Callout loop not allowed".
And so I have the problem described in this blog: http://codrspace.com/gwickman/callout-loop-not-allowed-error-when-using-getcontent-and-getcontentaspdf/
Can someone please help me get around this error or help with another way to create PDF attachments for emails in a batch job?
Thank you very much
You need to create a WS
Add session to the constructor
And call the function in your Batch class 'execute function' And call your batch : i hope it solve the problem.
Thanks very much!
Can you exlain how it will solve this problem -
"As of Summer 15, Salesforce have implemented a critical update "PageReference getContent() and getContentAsPDF() Methods Treated as Callouts". Once enabled, you may get the rather uninformative error "(304497466)|FATAL_ERROR|System.CalloutException: Callout loop not allowed".
Many thanks
Sean
eg.
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
//add other variables (subject etc)
String body='<html> <body> </body> </html>'
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName('filname.pdf');
efa.Body = Blob.toPDF(body);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
So you build up your string using html and then set as a Blob and then add that as an attachment to your email in Apex. This will then create a pdf attachment that you can use in a trigger or Batch job. It works for me! The only issue is that you need to spend a lot more time on formatting as certain CSS does not come through!
I had to rebuild all the filters etc from my report using SOQL in apex and then build the body that way - ie using loops etc. It is a bit tedious but it works.
the issue will be solved ine the next release "Winter 16"
http://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_apex_pagereference_getcontent.htm#rn_apex_pagereference_getcontent
However, the sandbox also includes a critical update called: PageReference getContent() and getContentAsPDF() Methods Treated as Callouts
When I activate it the getContent breaks.
So: Is the update detritis from Summer? Could it be thier "fix"? Should it work after activating the update (implying I'm doing something wrong)?
What are your thoughts?
Prior to Winter 16 PageReference.getContent() would not work from asynchronous contexts (batch, @future, scheduled jobs).
One thing I did notice is that the critical update was activated by default in the pre-release Org.
Are you sure you weren't deactivating the critical update?
How exactly did the getContent() break? "Callout loop not allowed" or something else?
WARNING: We've been having some issues getting this to run successfully as a Force.com Site Guest User. We are still investigating a workaround for this.
http://docs.releasenotes.salesforce.com/en-us/spring16/release-notes/rn_vf_pagereference_getcontent_cruc.htm
I used your logic, but no luck.
This issue is solved is this link http://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_apex_pagereference_getcontent.htm#rn_apex_pagereference_getcontent
Can I have your email Id so that I can send you my class file for your help?
Thank you.
PageReference p = Page.MyCustomPage;
Blob appBlob;
if(Test.IsRunningTest()) {
appBlob = Blob.valueOf('Test PDF');
} else {
appBlob = p.getContent();
}
and the page has a header like:
<apex:page controller="MyCustomPageController" applyHtmlTag="false" applyBodyTag="false" renderAs="pdf" showHeader="false" sidebar="false" standardStylesheets="false">
Hope this helps,
Dan
I am getting the error:System.CalloutException: You have uncommitted work pending. Please commit or rollback before calling out
The reason I believe is due to the getcontent() callout within a for loop.
This was working fine till the Summer 15 critical update.
If i run the code for a single list item,it works.It fails when it starts iterating.
Any assistance will be appreciated.
global class Exporter implements System.Schedulable
{
global void execute(SchedulableContext sc) //USED FUTURE ANNOTATION//
{
CalloutExport();
}
@future(callout=true)
public static void CalloutExport()
{
List<Exporter__c> exportList = new List<Exporter__c>{};
Datetime dt = DateTime.newInstance(Date.today(), Time.newInstance(0, 0, 0, 0));
Date d = Date.today();
Date firstDate = d.toStartOfMonth();
Date lastDay = firstDate.addDays(Date.daysInMonth(d.year(), d.month())-1);
system.debug('First Day: ' + firstDate);
system.debug('Last Day: ' + lastDay);
exportList = [Select Id, Name, Report_ID__c, Attachment_Name__c,Email_Body__c,Email_Subject__c, Email_Recipients__c, Frequency__c, Weekly_Days__c, Monthly_Day__c,Last_Run_DTM__c from Exporter__c
where Last_Run_DTM__c != today ];
for(Exporter__c e : exportList){
//Determine if Exporter record is scheduled to run today.
Boolean process = Test.isRunningTest() ? true : false;
//Test for Weekly frequency.
process = e.Frequency__c == 'Weekly' && e.Weekly_Days__c.contains(dt.format('EEEE')) ? true : process;
//Test for Monthly frequency.
process = e.Frequency__c == 'Monthly' && (e.Monthly_Day__c == String.valueOf(d.day()) || e.Monthly_Day__c == 'last' && d == lastDay || lastDay.day() <= Integer.valueOf(e.Monthly_Day__c)) ? true : process;
//Run process if scheduled.
if(process){
System.debug('Starting message processing for: ' + e.Name);
ApexPages.PageReference report = new ApexPages.PageReference('/' + e.Report_ID__c + '?csv=1');
Messaging.EmailFileAttachment attachment = new Messaging.EmailFileAttachment();
attachment.setFileName(e.Attachment_Name__c);
Blob content = report.getContent();//THIS IS WHERE IT GET'S THE EXCEPTION//
attachment.setBody(content);
attachment.setContentType('text/csv');
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment});
message.setSubject(e.Email_Subject__c);
message.setPlainTextBody(e.Email_Body__c);
String[] emailRecipients = e.Email_Recipients__c.split(',');
message.setToAddresses(emailRecipients);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {message});
System.debug('Completed message processing for: ' + e.Name);
e.Last_Run_DTM__c=Datetime.now();
update e;
}
}
}
}