Good day everyone. I am trying to create a pdf from a newly created record. I search in the internet and I find this link : https://jungleeforce.wordpress.com/2014/06/11/generate-a-pdf-and-attach-it-to-record-from-a-trigger-in-salesforce-restful/ . What it did is that it create a trigger that will call a future class and the future class call rest webservice. I follow everything instructed in the link but maybe because it's now outdated, I encounter an error saying callout loop not allowed
. This error occur on the pdf.getContent();
inside my rest webservice. Can you please tell me what are the things needed for this ? Or does anyone have a good article we can follow to fix this problem ? Below is my code. Thank you
Trigger:
trigger triggerOne on Subscription__c (after insert) {
for(Subscription__c sub : Trigger.New) {
allSubsIdforPDF.add(sub.Id);
}
pdfTriggerController.addPDFAttach(userInfo.getSessionId(), allSubsIdforPDF);
}
pdfTriggerController:
global class pdfTriggerController{
@Future(callout=true)
public static void addPDFAttach(string sessionId, list<id> IdList){
HttpRequest req = new HttpRequest();
req.setEndpoint('https://'+URL.getSalesforceBaseUrl().getHost()+'/services/apexrest/addPDFtoSubscription/');
req.setMethod('POST');
req.setBody('{"subsIdList":'+JSON.serialize(IdList)+'}');
req.setHeader('Authorization', 'Bearer '+ sessionId);
req.setHeader('Content-Type', 'application/json');
Http http = new Http();
if(!test.isRunningTest()){
HTTPResponse res = http.send(req);
}
}
}
REST Webservice:
@RestResource(urlMapping='/addPDFtoSubscription/*')
global class theaddPDFtoSubscription{
@HttpPost
global static void doPost(list<Id> subsIdList) {
System.debug('subsIdList :' + subsIdList);
list<attachment> insertAttachment = new list<attachment>();
for(Id subsId: subsIdList){
//create a pageReference instance of the VF page.
pageReference pdf = Page.Membership_Certificate_PDF;
//pass the Account Id parameter to the class.
pdf.getParameters().put('id',subsId);
Attachment attach = new Attachment();
Blob body;
if(!test.isRunningTest()){
body = pdf.getContent();
}else{
body=blob.valueOf('TestString');
}
attach.Body = body;
attach.Name = 'pdfName'+subsId+'.pdf';
attach.IsPrivate = false;
attach.ParentId = subsId;//This is the record to which the pdf will be attached
insertAttachment.add(attach);
}
//insert the list
insert insertAttachment;
}
}
VF:
<apex:page standardController="Subscription__c" renderAs="pdf">
Hey, this is a sample
</apex:page>