Send pdfContent using Trigger in salesforce
Requirement: I have VF page on render as pdf means it is PDF file. I want to send this
pdf when in my object there is field "isSend" checkbox type is equal to true then send to users email ids ,
Send PDF using trigger using Web Service Class.
you need to create two class and one trigger. And your VF Page Pdf class and page.
Step 1: Create GETPDFContentController Class
/*
Before this you need to create a VFPage
*/
/**
* Created By### refernce No:20180555 : Sumit Shukla
* Description : This Apex class based on REST API which exposes POST method to send Email with
* pdf content
*/
@RestResource(urlMapping='/sendPDFEmail/*')
global class GETPDFContentController{
@HttpPost
global static void sendEmail(String EmailIds, String Subject, String body, string recordID) {
List<String> EmailIds = EmailIds.split(',');
PageReference ref = new PageReference('/apex/VFPageName?id='+recordID); // Firstly you need to create a VF page render as pdf and Passing the record id in url
Blob b = ref.getContentAsPDF();
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
Messaging.EmailFileAttachment efa1 = new Messaging.EmailFileAttachment();
efa1.setFileName('PdfName.pdf');
efa1.setBody(b);
String addresses;
email.setSubject(Subject);
email.setToAddresses( EmailIds);
//email.setPlainTextBody(Body);
email.setHtmlBody(body);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa1});
Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
Step 2: Create SendVFAsAttachmentController Class
/**
* Created By### refernce No:20180556 : Sumit Shukla
* Description : This class exposes @future method to send VF page rendered as PDF as attachment in Email
*/
public class SendVFAsAttachmentController{
@future(callout=true)
public static void sendVF(String EmailIds, String Subject,String body, string recordID, String userSessionId)
{
//Replace below URL with your Salesforce instance host
// Add URL in address and also add in Remote site Setting
// for sandBox and production it will be differnet . SO add url in custom label
// String url = Label.SendEmailURL; // SendEmailURL is Custom label the is URL is written
String addr = 'https://cs6.salesforce.com/services/apexrest/sendPDFEmail'; // https://cs6.salesforce.com/ sandBox Url or Prod/dev Url
// String addr = url;
HttpRequest req = new HttpRequest();
req.setEndpoint( addr );
req.setMethod('POST');
req.setHeader('Authorization', 'OAuth ' + userSessionId);
req.setHeader('Content-Type','application/json');
Map<String,string> postBody = new Map<String,string>();
postBody.put('EmailIds',EmailIds);
postBody.put('Subject',Subject);
postBody.put('body',body);
postBody.put('recordID',recordID);
String reqBody = JSON.serialize(postBody);
req.setBody(reqBody);
Http http = new Http();
HttpResponse response = http.send(req);
}
}
Step 3: Create Trigger on Object
/**
* Created By### refernce No:20180556 : Sumit Shukla
* Description : This trigger has the condition on that u want to send pdf
*/
Now Write the Trigger on which object you want to send the Pdf connect
string strid;
// write the condition as you required
if(trigger.isAfter && trigger.isUpdate)
{
string body ='';
for(Sobject obj : trigger.new)
{
if(isSend ==true)
strid = obj.id;
}
body = 'Dear Sir/Madam, <br/><br/>';
body += 'Greetings!! <br/> <br/>';
body += 'Thanks for your order.<br/> <br/>';
body += 'Thank You<br/>';
body += 'Regards<br/>';
body += 'Sumit Shukla';
SendVFAsAttachmentController.sendVF('Sumitshukla.mca@gmail.com,sumit@gmail.com,shukla@gmail.com',' Subject Name', body, strid , UserInfo.getSessionId());
}
}
}
Comments
Post a Comment