You need to sign in to do that
Don't have an account?
Felix Quinones
Why my ApexTrigger is not working? Error: sendPdfAsAttachement.
Hello All,
I'm trying to automatically send a PDFPage as an attachment every time a new grant opportunity is created or StageName == 'Vetting'.
The following is my ApexTrigger.
trigger SendPDFOpportunity on Opportunity (after insert, after update) { // Check if the opportunity is newly created or marked as vetting for (Opportunity opp : Trigger.new) { if ((opp.IsClosed && opp.IsWon) || opp.StageName == 'Vetting') { // Send the PDF as an attachment sendPdfAsAttachment(opp.Id); } } } private void sendPdfAsAttachment(Id opportunityId) { // Generate the PDF PageReference pdfPage = Page.NewGrantOpportunity; pdfPage.getParameters().put('id', opportunityId); Blob pdfBlob = pdfPage.getContent(); // Create the attachment Attachment pdfAttachment = new Attachment(); pdfAttachment.Name = 'Opportunity.Name + '.pdf'; pdfAttachment.Body = pdfBlob; pdfAttachment.ContentType = 'application/pdf'; pdfAttachment.ParentId = opportunityId; insert pdfAttachment; }
It is currently showing errors in line 6 and 11.
Line 6: Method does not exist or incorrect signature: void sendPdfAsAttachment(Id) from the type SendPDFOpportunity
Line 11: Missing '<EOF>' at 'private'
I have been trying everything I could. Any recommendations for me?
Can you confirm in which class is the method sendPdfAsAttachment present?
Is it on same Trigger? If so this is not correct way. You have to create a class and add this method there and call that from Trigger.
Thanks,
Hello Sai,
Thank you for your comment.
That is what I think I did after reviewing the code.
I currently have visualforce page, an apex class, and an apex trigger. However, now I'm recevinf the following error:
We hit a snag.
Review the errors on this page.
SendPDFOpportunity: execution of AfterInsert caused by: System.VisualforceException: Getting content from within triggers is currently not supported. Class.SendPDFAttachment.sendPdfAsAttachment: line 7, column 1 Trigger.SendPDFOpportunity: line 6, column 1
What I'm doing wrong?
My Apex Codes are below.
Visualforce page named: NewrantOpportunity
I have an Apex Class named "SendPDFAttachment"
Finally, I have an Apex Trigger named "SendPDFOpportunity"