You need to sign in to do that
Don't have an account?
Email Template In A Bulk Script Losing StyleSheet
We are trying to run a script that calls an Email Template(Code Below) and creates a list of Messaging.SingleEmailMessage for a custom object and emails the SingleEmailMessage list to customers.
The issue we ran into was after the first email was sent out, the rest of the email PDF attachment loses it reference to the style sheet.
Script Code:
List<CustomObject>invoices = [select id
,name
,Email
,OwnerId
from CustomObject
where already_sent__c = true];
User u = [select email
,Name
from User
where name like '%Admin%'];
EmailTemplate invEmail = [select id
from EmailTemplate
where name = 'correctCustomObject'];
List<Messaging.SingleEmailMessage>emails = new List<Messaging.SingleEmailMessage>();
for (SCMC__Invoicing__c invoice : invoices){
String invoiceEmailAddr = invoice.Email;
String ownerEmail = u.email;
String[] toAddresses = new String[] {invoiceEmailAddr};
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(toAddresses);
mail.setReplyTo(ownerEmail);
mail.setSenderDisplayName(u.Name);
mail.setUseSignature(false);
mail.setTemplateId(invEmail.id);
mail.setTargetObjectId(invoice.OwnerID);
mail.setSaveAsActivity(false);
mail.setWhatId(invoice.id);
emails.add(mail);
}
try {
Messaging.sendEmail(emails);
} catch(Exception ex) {
System.debug(ex.getMessage());
}
Visual Force Email Template:
<messaging:emailTemplate subject="Your Corrected PDF" relatedToType="Custom Object">
<messaging:plainTextEmailBody >
Text Here!
</messaging:plainTextEmailBody>
<messaging:htmlEmailBody >
Text Here!
</messaging:htmlEmailBody>
<messaging:attachment renderAs="PDF" filename="{!RelatedTo.name}.pdf">
<c:Layout invId="{!RelatedTo.id}" />
</messaging:attachment>
</messaging:emailTemplate>
Layout Component:
<apex:component access="global" controller="ControllerClass" layout="none">
<apex:attribute name="idValue" type="string" required="false" assignTo="{!idValue}" description="The id of the item being rendered."/>
<apex:stylesheet value="{!$Resource.FormsStyleSheetExtension}" />
<apex:componentBody >
Body of the custom object be display
</apex:componentBody >
</apex:component>
I've even moved the stylesheet resource inside the email template and that did not work.
Has anyoneseen this issue before? And does anyone have a work around?
Hi,
Please write / place your CSS in the Email body itself or some host the CSS in some where public site.
As your CSS is inside the Static Resource, The user viewing the Email is not logged into salesforce and therefore the Static resource URL is forbidden.
I hope, it would help you.
Problem with that is we are talking about PDF that is getting created before email the customer. Also, why does it work the first time and not the others? You would think URL is forbidden would be for everyone.