function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Web-passWeb-pass 

Issue with emailing PDF attachment

Hello,

 

I have a Detailed Page Button, which onClick runs JavaScript and calls out a method inside a global class. Here is the code for that:

 

{!REQUIRESCRIPT("/soap/ajax/15.0/connection.js")}
{!REQUIRESCRIPT("/soap/ajax/15.0/apex.js")}

sforce.apex.execute( "EmailInvoiceController", "emailinvoice", {invid:"{! Invoice__c.Id }"});

 

The global class EmailInvoiceController is as follows.

 

global class EmailInvoiceController
{
    @future(callout=true)
    webservice static void emailinvoice(ID invid)
    {
// code which fetches the Visualforce page and emails a pdf
}
}

It works perfectly fine and i get an email with a pdf attachment when the button is clicked. Now i run a trigger that calls this method every time a record is inserted. The problem i am facing here is that the email that i receive due to the trigger has a pdf attachment which is malformed and cannot be opened. Also the size of the attachment is reduced to 1.1 kb from the expected 20 kb.

 

I have not changed the code whatsoever and the same code gives perfect results on button click, but not when the method is called from a trigger.

 

Any directions on this issue are highly appreciated.

 

Thanks !!

- P 

 

 

Jeremy_nJeremy_n

I'm guessing you're using getContentAsPDF. From the Apex Developers Guide:

 

 

The getContent and getContentAsPDF PageReference methods aren't allowed in triggers.

 

 

I don't think what you're trying to do is possible. You could include a URL in the email that would open the PDF when the user clicks on it, but no PDF attachments.

 

Jeremy

Web-passWeb-pass

Hey,

 

Thank you for your reply. Yes, i am using getContent to get the data. So here, instead of using a trigger if i schedule this class as a cron job, will it work? I suppose i will be getting the same error.

 

- P