I have a trigger which sends a mail on update of the approval status . I am using a HTML part in that trigger to send the mail .

enter image description here

In place of x I want to call my visualforce component which is fetching those values through a controller . What is the format to call the component there


I think it would be better to create a email template and then pass the template id and what id to fetch the fields values. This makes it easy if you just want to modify the template only in future also the code looks clean. You just have to add these method in Messaging.SingleEmailMessage

  1. setTemplateID(emailTemplate.Id);
  2. setWhatId(account.Id);//record if for merging the fields.

So the code becomes something like this:

public static void sendNotification(Contact){
   Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();

   message.setTargetObjectId(contact.id); //The ID of the contact, lead, or user to which the email will be sent

  EmailTemplate emailTemplate = [Select Id,Subject,Description,HtmlValue,DeveloperName,Body from EmailTemplate where name = ‘Test Account Alert Email’];
  message.setWhatId(account.Id); //for mergin fields
  message.toAddresses = new String[] { contact.email};
  Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {message};
  Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);

 if (results[0].success) 
   System.debug('The email was sent successfully.');
 } else {
   System.debug('The email failed to send: ' +  results[0].errors[0].message);
  • but i think if we use template it will be mandatory to use set.TargetObjectId() and as I need to pass the external ids as well I cant pass ids in it – user75937 Dec 6 '19 at 10:06
  • In setTargetObjectId(targetObjectId) you can also add user id. The email will then be sent to that salesforce user – summer Dec 6 '19 at 12:02
  • yes i can do that but I also have external email ids so I cant have their ids – user75937 Dec 9 '19 at 6:36
  • you can use setToAddresses(toAddresses) that will accept any email list along with setTreatTargetObjectAsRecipient(false). – summer Dec 9 '19 at 10:24

add <c:vfcomponentname/>. If there are any parameters to be passed for instance related contact , use <c:vfcomponentname contact={!relatedTo.Contact__c}/>

  • I tried adding it ,syntax issue . – user75937 Dec 6 '19 at 11:18
  • what have you tried and and what is the error ? – devforce Dec 6 '19 at 11:52
  • I tried adding <c:vfcomponentname contact={!relatedTo.Contact__c}/> in place of <x> in above code . The error was it cant accept '< ' – user75937 Dec 9 '19 at 6:35
  • You have to break your string.You can try something like String approvalDetails='<html>...The following'+<c:vfcomponentname contact={!relatedTo.Contact__c}/>+'Request has been approved....</html>' – devforce Dec 9 '19 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.