You need to sign in to do that
Don't have an account?
Jyothy Lakshmy
How can send a visualforcepdf page as attachment while insert / update a record?
How can send a visualforcepdf page as attachment while insert / update a record?
I have a requrement to send a visualforce PDF as an email attachemnt on insert a record A.
I am getting the error : " Update failed. First exception on row 0 with id a8d60000000TNzVAAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, IDS_SOShipperTrigger: execution of AfterUpdate caused by: System.VisualforceException: Getting content from within triggers is currently not supported".
My code is given below :
trigger IDS_SOShipperTrigger on rstk__soship__c (before insert, before update, after insert, after update) {
Set<Id> shipperIds = new Set<Id>();
Set<Id> vendorShippedShipperIds = new Set<Id>();
Map<Id, String> shipperVendorEmails = new Map<Id, String>();
System.debug('********************777');
if(Master_Switches__c.getInstance('00e600000017Rhf').Triggers_On__c) {
System.debug('********************666');
if(Trigger.isBefore) {
for(rstk__soship__c shipper : Trigger.New) {
if(shipper.rstk__soship_closed__c == True
&& Trigger.oldMap.get(shipper.Id).rstk__soship_closed__c == False) {
shipper.Shipment_Date__c = System.Today();
}
}
}
else if(Trigger.isAfter) {
for(rstk__soship__c shipper : Trigger.New) {
if(shipper.rstk__soship_closed__c == True
&& Trigger.oldMap.get(shipper.Id).rstk__soship_closed__c == False) {
shipperIds.add(shipper.Id);
}
}
//System.debug('********************555');
if(shipperIds.size() > 0) {
//Check whether shipment is sent by a vendor or not.
for(rstk__sydata__c sydata : [SELECT Id, rstk__sydata_soship__c
FROM rstk__sydata__c
WHERE rstk__sydata_soship__c IN :shipperIds]) {
vendorShippedShipperIds.add(sydata.rstk__sydata_soship__c);
}
System.debug('********************444');
if(vendorShippedShipperIds.size() > 0) {
for(rstk__soshipline__c shipperLine : [SELECT Id, rstk__soshipline_shipper__c,
rstk__soshipline_line__r.PO_Line__r.rstk__poline_ordno__r.rstk__pohdr_vendno__r.Auto_Packing_List_Email__c
FROM rstk__soshipline__c
WHERE rstk__soshipline_shipper__c IN :vendorShippedShipperIds]) {
if(shipperLine.rstk__soshipline_line__r.PO_Line__r.rstk__poline_ordno__r.rstk__pohdr_vendno__r.Auto_Packing_List_Email__c != null) {
shipperVendorEmails.put(shipperLine.rstk__soshipline_shipper__c,
shipperLine.rstk__soshipline_line__r.PO_Line__r.rstk__poline_ordno__r.rstk__pohdr_vendno__r.Auto_Packing_List_Email__c);
}
}
System.debug('********************333');
if(shipperVendorEmails.size() > 0) {
//Send emails as pdf attachment
Boolean successSend = True;
String emailResult = '';
List<String> selContactEmails = new List<String>();
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
transient Messaging.EmailFileAttachment [] pdfAttachmentList = new List<Messaging.EmailFileAttachment> () ;
PageReference invoicePage = null;
invoicePage = Page.IDS_PackingSlip;
selContactEmails.add('jyothy@qburst.com');
System.debug('********************222');
for(String shipperIdValue : shipperVendorEmails.keySet()){
invoicePage.getParameters().put('Id', shipperIdValue);
invoicePage.setRedirect(true);
Blob invoiceContent = !Test.isRunningTest() ? invoicePage.getContent() : Blob.ValueOf('dummy text');
Messaging.EmailFileAttachment pdfAttachment = new Messaging.EmailFileAttachment();
pdfAttachment.setFileName('Packing Slip ' + ' - ' + shipperIdValue + '.pdf');
pdfAttachment.setBody(invoiceContent);
pdfAttachment.setContentType('application/pdf');
pdfAttachmentList.add(pdfAttachment);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
selContactEmails.add(shipperVendorEmails.get(shipperIdValue));
email.setToAddresses(selContactEmails);
email.setSubject('Packing Slip '+ ' - ' + shipperIdValue );
email.setPlainTextBody(Label.Invoice_Email_Body);
if(pdfAttachmentList != null) {
email.setFileAttachments(pdfAttachmentList) ;
}
emailList.add(email);
System.debug('********************111');
}
if(emailList.size() > 0){
Messaging.SendEmailResult[] sendResults = Messaging.sendEmail(emailList);
//Get error message if any
for(Messaging.SendEmailResult result : sendResults) {
if(!result.isSuccess()) {
emailResult = result.getErrors()[0].getMessage();
}
}
if(String.isBlank(emailResult)){
emailResult = Label.IDS_Invoice_Sent_Successful_Message;
successSend = false;
}
}
}
}
}
}
}
}
Any help? Thanks in advance :)
I have a requrement to send a visualforce PDF as an email attachemnt on insert a record A.
I am getting the error : " Update failed. First exception on row 0 with id a8d60000000TNzVAAW; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, IDS_SOShipperTrigger: execution of AfterUpdate caused by: System.VisualforceException: Getting content from within triggers is currently not supported".
My code is given below :
trigger IDS_SOShipperTrigger on rstk__soship__c (before insert, before update, after insert, after update) {
Set<Id> shipperIds = new Set<Id>();
Set<Id> vendorShippedShipperIds = new Set<Id>();
Map<Id, String> shipperVendorEmails = new Map<Id, String>();
System.debug('********************777');
if(Master_Switches__c.getInstance('00e600000017Rhf').Triggers_On__c) {
System.debug('********************666');
if(Trigger.isBefore) {
for(rstk__soship__c shipper : Trigger.New) {
if(shipper.rstk__soship_closed__c == True
&& Trigger.oldMap.get(shipper.Id).rstk__soship_closed__c == False) {
shipper.Shipment_Date__c = System.Today();
}
}
}
else if(Trigger.isAfter) {
for(rstk__soship__c shipper : Trigger.New) {
if(shipper.rstk__soship_closed__c == True
&& Trigger.oldMap.get(shipper.Id).rstk__soship_closed__c == False) {
shipperIds.add(shipper.Id);
}
}
//System.debug('********************555');
if(shipperIds.size() > 0) {
//Check whether shipment is sent by a vendor or not.
for(rstk__sydata__c sydata : [SELECT Id, rstk__sydata_soship__c
FROM rstk__sydata__c
WHERE rstk__sydata_soship__c IN :shipperIds]) {
vendorShippedShipperIds.add(sydata.rstk__sydata_soship__c);
}
System.debug('********************444');
if(vendorShippedShipperIds.size() > 0) {
for(rstk__soshipline__c shipperLine : [SELECT Id, rstk__soshipline_shipper__c,
rstk__soshipline_line__r.PO_Line__r.rstk__poline_ordno__r.rstk__pohdr_vendno__r.Auto_Packing_List_Email__c
FROM rstk__soshipline__c
WHERE rstk__soshipline_shipper__c IN :vendorShippedShipperIds]) {
if(shipperLine.rstk__soshipline_line__r.PO_Line__r.rstk__poline_ordno__r.rstk__pohdr_vendno__r.Auto_Packing_List_Email__c != null) {
shipperVendorEmails.put(shipperLine.rstk__soshipline_shipper__c,
shipperLine.rstk__soshipline_line__r.PO_Line__r.rstk__poline_ordno__r.rstk__pohdr_vendno__r.Auto_Packing_List_Email__c);
}
}
System.debug('********************333');
if(shipperVendorEmails.size() > 0) {
//Send emails as pdf attachment
Boolean successSend = True;
String emailResult = '';
List<String> selContactEmails = new List<String>();
List<Messaging.SingleEmailMessage> emailList = new List<Messaging.SingleEmailMessage>();
transient Messaging.EmailFileAttachment [] pdfAttachmentList = new List<Messaging.EmailFileAttachment> () ;
PageReference invoicePage = null;
invoicePage = Page.IDS_PackingSlip;
selContactEmails.add('jyothy@qburst.com');
System.debug('********************222');
for(String shipperIdValue : shipperVendorEmails.keySet()){
invoicePage.getParameters().put('Id', shipperIdValue);
invoicePage.setRedirect(true);
Blob invoiceContent = !Test.isRunningTest() ? invoicePage.getContent() : Blob.ValueOf('dummy text');
Messaging.EmailFileAttachment pdfAttachment = new Messaging.EmailFileAttachment();
pdfAttachment.setFileName('Packing Slip ' + ' - ' + shipperIdValue + '.pdf');
pdfAttachment.setBody(invoiceContent);
pdfAttachment.setContentType('application/pdf');
pdfAttachmentList.add(pdfAttachment);
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
selContactEmails.add(shipperVendorEmails.get(shipperIdValue));
email.setToAddresses(selContactEmails);
email.setSubject('Packing Slip '+ ' - ' + shipperIdValue );
email.setPlainTextBody(Label.Invoice_Email_Body);
if(pdfAttachmentList != null) {
email.setFileAttachments(pdfAttachmentList) ;
}
emailList.add(email);
System.debug('********************111');
}
if(emailList.size() > 0){
Messaging.SendEmailResult[] sendResults = Messaging.sendEmail(emailList);
//Get error message if any
for(Messaging.SendEmailResult result : sendResults) {
if(!result.isSuccess()) {
emailResult = result.getErrors()[0].getMessage();
}
}
if(String.isBlank(emailResult)){
emailResult = Label.IDS_Invoice_Sent_Successful_Message;
successSend = false;
}
}
}
}
}
}
}
}
Any help? Thanks in advance :)
it's not possible to do it directly from trigger (call method getContent()).
try workaround:
- create REST class/method, to send this attachment
- create a @future method, and call your REST class from within this method
- call that future method from your trigger
We've done something similiar in our project, works fine :)
Marek