5

Is it possible to access LetterHead in apex.I need to put the letter head in sethtmlbody method.How to access LetterHead.

UPDATED

Here the bt.value is displaying blank when contact is receiving an email but when I debug the value of bt.value it is showing some html code in it

         BrandTemplate bt = [select id,Value from BrandTemplate where name='Standard Letterhead' limit 1]; 
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         String[] toAddresses = new String[] {Contact_Email__c}; 
         mail.setToAddresses(toAddresses);
         mail.setSenderDisplayName('Support');
         mail.setSubject('Invoice');
         mail.setBccSender(false); 
         mail.setUseSignature(false);
         String messageBody = '<html>'+ bt.Value + '<body>'+ '<p>Dear '+ o.Contact__r.Name + ': </p>'+'Some TEXT'
         mail.setHtmlBody(messageBody)          
         Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
5
  • please look at the update answer
    – Rao
    Commented Apr 22, 2015 at 21:05
  • is there a reason why you are avoiding template? are you trying to construct your template dynamically? I am not seeing the letter head images show up on my email too. I tried to make the images available publicly, loaded into a document. No luck yet!!!
    – Rao
    Commented Apr 23, 2015 at 17:09
  • yes I am trying to construct template dynamically.
    – sfdc
    Commented Apr 23, 2015 at 17:11
  • @rao Any luck on this.
    – sfdc
    Commented Apr 23, 2015 at 19:49
  • @rao I have uploaded header and footer of LetterHead to Documents and made it accessible externally and used it in trigger and it works.Thanks
    – sfdc
    Commented Apr 24, 2015 at 20:56

1 Answer 1

7

https://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_brandtemplate.htm

BrandTemplate bt = [select id from BrandTemplate where name='Blank' limit 1];

Update:

A letter head goes in conjunction with HTML Email template. So to access letterhead from email template query

emailtemplate et = [select BrandTemplateId from emailtemplate where brandtemplateId.name='Blank' limit 1];
system.debug('## Email template's letter head :'+ et.brandtemplateId);

To answer the second part of your question I need to put the letter head in sethtmlbody method.How to access LetterHead.

AFAIK you cannot do this when you try to use together

mail.sethtmlbody();
mail.settemplateid();

you will get get an error

System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body, html body, subject and charset may not be specified : []

7
  • welcome back Rao Commented Apr 22, 2015 at 20:21
  • @MohithShrivastava Thanks, too busy at work have be on and off :)
    – Rao
    Commented Apr 22, 2015 at 20:22
  • As long as you're not using merge fields in the email template, you can probably bring your BrandTemplate and EmailTemplate together in code using just sethtmlbody() and String.replace() to shove the EmailTemplate into the correct location in the BrandTemplate. Commented Apr 22, 2015 at 21:25
  • BrandTemplate bt = [select id,Value from BrandTemplate where name='Standard Letterhead' limit 1]; String messageBody = '<html>'+ bt.Value + '<body>'+ '<p>Dear '+ o.Contact__r.Name + ': </p>';
    – sfdc
    Commented Apr 22, 2015 at 21:31
  • Here the bt.value is displaying blank when contact is receiving an email but when I debug the value of bt.value it is showing some html code in it
    – sfdc
    Commented Apr 22, 2015 at 21:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .