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
LogeshLogesh 

Sending an email with the pdf attachment through apex trigger.

Hi Friends,

 

                    I have a scenario where I need to send the email with the pdf attachment once the record is insert (this is for custom object).

Record for the custom object will be inserted when an Event is created with particular condition(This is done through trigger).

 

I wrote an apex class and I called that class from the trigger. It worked fine. I used the getContentasPdf to render the record inputs as pdf  to send as attachment. But the "getContentasPdf" is not supported in triggers. Is there any other way to send a email with pdf as attachment from trigger. Please help me!!

 

- Logesh

Best Answer chosen by Admin (Salesforce Developers) 
LogeshLogesh

Hi all,

 

I accomplished my scenario by creating a workflow rule on custom object with email alert. I created the attachment in the email template(tag - <messaging:attachment> --> refer visualforce developer guide). So, the flow goes on like this 

 

Whenever the event is created, a record will be created for the custom object and that triggers the workflow rule for that custom object on the workflow rule evaluation criteria - only when a record is created.

 

Cheers,

Logesh

All Answers

ngabraningabrani

There could be couple of work-arounds for this -

a) You could invoke a batch class and have the batch class have the functionality to send the email as an attachment.

b) You could invoke a future method from the trigger and that future method could have this functionality.

LogeshLogesh

Thanks ngabrani!!

 

I tried with the "@future" annotation. Email invoked with the pdf attachment. But the pdf doesnt contain any content. It is blank.

 

This is my code:

cus_obj cus_obj_query = [Select Id, .. where Id =: (passed Id as an argument from trigger)];

 

 pdf.getParameters().put('id', cus_obj_query.Id);   
            //System.debug('checkParam '+pdf.getParameters().put('id', cus_obj_query.Id));        
            //pdf.setRedirect(true);
            
            Blob b = pdf.getContentAsPDF();
                      
            
           
            
            
            //System.debug('checkPdf '+pdf.getContentAsPDF());
            //System.debug('checkPage '+b);    
            Messaging.EmailFileAttachment fileAttachment = new Messaging.Emailfileattachment();                           
            //fileAttachment.setContentType('application/pdf');                   
            fileAttachment.setFileName(cus_obj_query.name + '.pdf');
            fileAttachment.setBody(b);
            mail.setFileAttachments(new Messaging.Emailfileattachment[]{fileAttachment});

 

// CODE ENDS

 

Is anything more.. i need to do to get the content in pdf. Please Help me!!

 

I also tried with HTTP request (referred from the post - "http://boards.developerforce.com/t5/Apex-Code-Development/Calling-getContent-from-a-trigger/td-p/168132").

 

It also didnt helped me!!

 

Regards,

-Logesh

 

LogeshLogesh

Hi all,

 

I accomplished my scenario by creating a workflow rule on custom object with email alert. I created the attachment in the email template(tag - <messaging:attachment> --> refer visualforce developer guide). So, the flow goes on like this 

 

Whenever the event is created, a record will be created for the custom object and that triggers the workflow rule for that custom object on the workflow rule evaluation criteria - only when a record is created.

 

Cheers,

Logesh

This was selected as the best answer
TresureTresure

Hello Logesh, i have similar requirement to send the contract as pdf to the client (Account) on Account creation, as pdf in auto generated manner.

 

Can you please help me if possible.