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
vepakvepak 

Not able to send PDF attachment via scheduled apex

Hi There,

 

I'm trying to send PDF attachment generated from Visualforce page through scheduled apex email class.

 

I can send PDF attachment using the Email client, but when I am trying to send that automatically using the scheduler PDF attachment is being corrupted 

 

Could you please help me in this.

 

Thanks.

Vamsi.

Best Answer chosen by Admin (Salesforce Developers) 
vepakvepak

here is the alternate method to achieve this.

 

http://wiki.developerforce.com/index.php/Visualforce_EmailQuote2PDF

All Answers

MikeGillMikeGill
When you say corrupted, do you the PDF contents or the attachment is not recognized as a PDF?

Can you post your code?
vepakvepak

Mike,

 

File is recognized as PDF but content of it is corrupted. When I try to open the file it says FAIL TO LOAD PDF DOCUMENT.

 

here is the code

global with sharing class scheduledinvoices implements Schedulable{
global ApexPages.StandardController controller;
global Invoice__c[] inv { get; set;}

global PageReference pdf { get; set;}

global void execute(SchedulableContext SC) {

inv=[select id, name, Email__c from WPOSP__Invoice__c where 
                             Set_recurring_payments__c=true];

for(Invoice__c si: inv){

pdf = Page.Recurring_Invoice;
    // add parent id to the parameters for standardcontroller
    pdf.getParameters().put('id',(String)si.id);


    // the contents of the attachment from the pdf// returns the output of the page as a PDF

    Blob b= pdf.getContent();
     
     
    Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
   
    attach.setFileName(si.name+'.pdf');
  
    attach.setBody(b);
 string[] toaddress = si.WPOSP__Email__c.split(',', 0);
 

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setUseSignature(false);
    mail.setToAddresses( toaddress );
    mail.setSubject('Your Statement');
    mail.setHtmlBody('Dear Customer,<br><br> Please find your statement attached in the email.');
    mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach }); 
 
    // Send the email
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
   
}
}
}

 

vepakvepak

The page Recurring_Invoice I'm calling in this class is working fine, I tested it.

 

 

MikeGillMikeGill
Recurring_Invoice is a visualforce page with renderas=pdf?

I assume loading individual pages outside the schedule class the PDF displays ok?

It's failing with getContents() part - I would need to try for myself. Can't try now, on the iPad.
vepakvepak

Mike,

 

You bet, Recurring_Invoice visualforce page is rendered as PDF and its displays fine outside the schedule class.

 

I'll be glad if you can try later and let me know.

 

Thanks for you time and support.

 

Vamsi. 

Ankit AroraAnkit Arora

This is really interesting, I will try this too.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

vepakvepak

Thank you very much for your interest Ankit. Hope we'll find some solution.

 

Thanks,

Vamsi.

MikeGillMikeGill

ok I'm setting this up now

MikeGillMikeGill

It's in the queue, will execute in 20 mins.

 

M

vepakvepak

mike, any luck? Could you please let me know the result so that we can work togather.

 

Thanks,

Vamsi.

MikeGillMikeGill

Failed to load the PDF - same thing, investigating some more

 

Question - Can force it to run immediately (rather than every hour)

 

M

vepakvepak

Dont know, I'll work on it and let you know I find any.

MikeGillMikeGill

This maybe a better solution

 

http://wiki.developerforce.com/index.php/Visualforce_EmailQuote2PDF

 

Still trying a few things - I think it's a bug

 

 

MikeGillMikeGill

No luck. I would raise a case. 

vepakvepak

Thanks for your time mike, I've just submitted a case.

 

Let's see how it goes. I'll keep you informed.

 

Thanks,

Vamsi.

vepakvepak

Hi Mike and Ankit,

 

As I said I opend a case on this and result is

 

You can't use the getContent and getContentAsPDFPageReference methods in scheduled Apex. 

 

ref: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

 

That is the reason everything is working fine except the attachment. They suggested to post this in ideas exchange so that salesforce will consider in implement it.

 

Thanks for your time.

 

Regards,

Vamsi.


Ankit AroraAnkit Arora

Indeed! How can I miss that. This is a limitation.

 

So post an Idea related to it, explaining a general scenario and post the link here. We all will promote it.

 

 

Thanks

Ankit Arora

Blog | Facebook | Blog Page

MikeGillMikeGill
Interesting....

I would add a flag "To Be Email", then set the flag with schedule which in turn fires individual emails to be sent out??

Or what about using template with visualforce page attached?

Can't believe there is not way to send out invoice PDF's on mass
vepakvepak

Guys,

 

here is the idea link

 

https://sites.secure.force.com/success/ideaView?c=09a30000000D9xtAAC&id=08730000000ZlnBAAS

 

Please promote and add you comments explaining further

vepakvepak

Mike,

 

As you suggested I'd try using template with visualforce page attached as explained here  http://wiki.developerforce.com/index.php/Visualforce_EmailQuote2PDF

 

Will let you know.

 

 

 

 

 

 

vepakvepak

Hike Mike and Ankit,

 

I tried tried what explained in above link, it worked fine. Sorry for the late reply.

 

Thanks,

Vamsi.

 

vepakvepak

here is the alternate method to achieve this.

 

http://wiki.developerforce.com/index.php/Visualforce_EmailQuote2PDF

This was selected as the best answer
Greg RohmanGreg Rohman

Hello.

 

Is there some way to utilize this method but for a Visualforce PDF that uses a custom controller?

 

-Greg

Greg RohmanGreg Rohman

I found a solution to sending a Visualforce PDF with a custom controller utilizing an email template and a Visualforce component. Here's the link to the solution thread:

 

http://boards.developerforce.com/t5/Apex-Code-Development/Sending-email-from-scheduled-apex/m-p/346409

 

-Greg

vepakvepak

Greg,

 

Thank you for sharing another solution. 

 

-Vamsi.