You need to sign in to do that
Don't have an account?
cedg
triggering apex method of extension controller
Hi,
i'm new in Trigger and Apex methods, so it's probably a dummy question.
I have an object SGRequest with a custom button to send PDF. (code found on the site of Jeff Douglas)
So i have a controller extension
public with sharing class SGRequestPDFEmail{
public SGRequest__c request {get;set;}
public SGRequestPDFEmail(Apexpages.Standardcontroller con) {
request = (SGRequest__c)con.getRecord();
}
public PageReference sendPdf() {
PageReference pdf = Page.SGRequestPDFTemplate;
// add parent id to the parameters for standardcontroller
pdf.getParameters().put('id',request.id);
// the contents of the attachment from the pdf
Blob body;
try {
// returns the output of the page as a PDF
body = pdf.getContent();
// need to pass unit test -- current bug
} catch (VisualforceException e) {
body = Blob.valueOf('Some Text');
}
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setContentType('application/pdf');
attach.setFileName(request.File_Name__c);
attach.setInline(false);
attach.Body = body;
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setUseSignature(false);
mail.setToAddresses(new String[] {request.email_author__c});
mail.setSubject('PDF Email Demo');
mail.setHtmlBody('Here is the email you requested! Check the attachment!');
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.INFO, 'Email with PDF sent to '+request.email_author__c));
return null;
}
}
This works fine, so when clicking on the button, the mail is correctly sent with the PDF in attachment.
Now i would like to trigger this after changing a field Status__c in my object SGRequest__c.
trigger SendPdfOnApproval on SGRequest__c (after update) {
for (SGRequest__c sgr : Trigger.new){
if ((sgr.Status__c != trigger.oldMap.get(sgr.id).Status__c) && (sgr.Status__c == 'Approved') ){
sgr.sendPdf();
}
}
}
but of course, sgr doesn't know the method sendPdf ...
Can anybody help me ?
Thanks in advance
You'll need to create it from your page/controller I'm afraid.
Again, from the docs regarding getcontent/getcontentaspdf
All Answers
I think you are out of luck on this one. The method that you want to call pulls the contents of a PageReference using getContent(). According to the Apex Developer's Guide:
The getContent and getContentAsPDF PageReference methods aren't allowed in triggers.
So it's not possible to send a PDF without an action button ?
You'll need to create it from your page/controller I'm afraid.
Again, from the docs regarding getcontent/getcontentaspdf
ok, thanks for the info