I have struck in a particular scenario where i need to showcase a data table of unrelated records in the email template. Initially i have created the Email Template. On click of a button in LWc im calling Apex Controller. In apex controller im Extracting the Data by using data save point but the values are getting stamped from the relatedObjectId record but the Other data from another Objects is not getting stamped. Can any one help me on this.

This is my lwc function which is calling apex

async getContactFieldsFunction(){
this.emailBodies = await getEmailList();}

This Is My VF email template

<messaging:emailTemplate subject="Embedding Apex Code" recipientType="User" relatedToType="Case">
<messaging:htmlEmailBody >
    <p>Hope this helps with the {!relatedTo.Status}.</p>
    <p>Hope this helps with the {!relatedTo.Origin}.</p>
     <p>Hope this helps with the {!relatedTo.SuppliedPhone}.</p>
     <p>As you requested, here's a list of all our Smith accounts:</p>
   <c:smithAccounts phoneNumberToBePassed="{!relatedTo.SuppliedPhone}"/>
    <p>Hope this helps with the {!relatedTo.SuppliedCompany}.</p>
    <p>Hope this helps with the {!relatedTo.Reason}.</p>

this is apex controller

public static String getEmailList() {
    String mailHtmlBodies;
        EmailTemplate emplaTemplate = [SELECT Id, Name, DeveloperName, NamespacePrefix, OwnerId, FolderId, FolderName, BrandTemplateId, EnhancedLetterheadId, TemplateStyle, IsActive, TemplateType, Encoding, Description, Subject, HtmlValue, Body, TimesUsed, LastUsedDate, CreatedDate, CreatedById, LastModifiedDate, LastModifiedById, SystemModstamp, ApiVersion, Markup, UiType, RelatedEntityType, IsBuilderContent FROM EmailTemplate WHERE Name = 'DEmo'];
        Case caseRecord = [SELECT Id,Status,Origin,SuppliedCompany,Reason FROM Case WHERE SuppliedCompany = 'zyzz'];
        system.debug('emplaTemplate- > '+emplaTemplate);
        system.debug('caseRecord- > '+caseRecord);
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{'[email protected]'};
        mail.setSenderDisplayName('Sample Email');
        system.debug('mail- > '+mail);
        Savepoint sp = Database.setSavepoint();
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
        String mailTextBody = mail.getPlainTextBody();
        mailHtmlBodies = mail.getHTMLBody();
        system.debug('mailHtmlBodies- > '+mailHtmlBodies);
        String mailSubject = mail.getSubject(); 
    }catch(exception e){
        system.debug( ' exception- '+e.getLineNumber()+ ' -  ' +e.getMessage()) ; 
    return mailHtmlBodies;   

this is my vf component

<apex:component controller="findSmithAccounts" access="global">
<apex:attribute type="String" id="oppId" name="phoneNumberToBePassed" assignTo="{!phoneNumber}" description="DeveloperName of Document"/>
    <apex:dataTable value="{!contactRecords}" var="s_contact">
    <apex:column headerValue="Contact Name">


this is my vf controller

public class findSmithAccounts {
public List<Contact> contactRecords { get; set; }
public String phoneNumber { get; set; }

public findSmithAccounts(){


public List<Contact> getSmithContacts() {
contactRecords =  [SELECT Id,Name FROM Contact WHERE Phone  = :phoneNumber LIMIT 10];
    return contactRecords;


  • does Case.SuppliedPhone actually have a value? And, SuppliedPhone might not be normalized and match any Contact.Phone (also possibly not normallized). I tend to normalize my phone numbers before attempting to match them using the E.164 standard
    – cropredy
    Commented Feb 11 at 18:42
  • Hello @cropredy , Thank you for replying and yes it does have a value and this was just a sample prototype.i mean the skeleton later on i will be replacing with the actual values.But my doubt is there any fault in my flow or any changes should i make? AS findSmithAccounts itself is not getting invoked. Commented Feb 12 at 5:45
  • why are you using the savepoint rollback? There is a better way to render the email body content. See Apex Messaging class, renderXXX methods
    – cropredy
    Commented Feb 12 at 19:19
  • I did use this renderStoredEmailTemplate and renderEmailTemplate function but those didnt gave me proper result. Commented Feb 13 at 11:26


You must log in to answer this question.