0

I have created a custom visual force email template as below. When I use this template, I am unable to retrieve {!recipient.Fieldname} values in the received email.

Flow: Create a new Event from a Lead > Workflow Triggers an email.

I am using a workflow and an email alert wf action for testing purposes.

VF Email template used:

<messaging:htmlEmailBody >
    <html>
        <body>

        <p>Dear {!recipient.FirstName},</p>
        <p>You have been invited to this event: {!relatedTo.Subject}.</p>
       <p>You can access the event at <a href = 
                    "https://b2b-dev1--inwidev1.lightning.force.com/{!relatedTo.id}">{!relatedTo.Subject}
                </a> </p>
        <p>Ref: {!recipient.id}.</p>
        </body>
    </html>
</messaging:htmlEmailBody>

I am receiving the following email with blank values for Lead fields: From: noreply@salesforce.com On Behalf Of Saibal Roy Sent: 04 November 2019 21:04 To: Roy, Saibal Subject: Sandbox: You have been invited to an event TEST TEST TEST TEST

"Dear , You have been invited to this event: TEST TEST TEST TEST. You can access the event at TEST TEST TEST TEST (hyperlink is working as expected) Ref: ."

what am I doing wrong? Kindly help.

Adding email alert screenshot. enter image description here

7
  • recipient (in VF email templates) can only point at Lead/Contact/User yet your email alert is on Event. Is recipient bound to {!Event.WhoId}?
    – cropredy
    Nov 4 '19 at 18:50
  • Yes. This event is created from Lead Detail Page. Hence, I believe that recipent is bound to WhoId. My requirement is to trigger an email as soon as an event is created from Lead/Opportunity or Quote. I have taken VF ET route as Event fields are not available as merge fields in vanilla email templates. Nov 4 '19 at 19:49
  • please use edit to amend your question: 1) what object is the WFR on? 2) what object is the email alert on? (Looks like Event) 3) what does the complete VF email template look like (i.e. definition of recipient and relatedTo)
    – cropredy
    Nov 4 '19 at 19:53
  • I have double checked qnd confirmed that WhoId is correctly populated. Nov 4 '19 at 20:09
  • I have figured it out. Will post the answer shortly. Nov 4 '19 at 20:50
1

Use a custom component to deal with the polymorphism of Event.WhoId

<messaging:emailTemplate subject="foo" relatedToType="Event">
<messaging:htmlEmailBody >
  <html>
    <body>
      <p>Dear <c:FirstName who="{!relatedTo.WhoId}"/>,</p>
    </body>
  </html>
</messaging:htmlEmailBody>
</messaging:EmailTemplate>

Apex component

<apex:component id="FirstNameId" controller="FirstNameComponentCtrl" access="global">
    <apex:attribute name="who" assignTo="{!whoId}" description="Event WhoId"
                    type="ID"/>
    <apex:outputText  value="{!firstName}"/>
</apex:component>

Apex component controller

public class FirstNameComponentCtrl {

   public Id whoId {get; set;}

   public String getFirstName() {
     if (whoId != null) {
       switch on String.valueOf(whoId.getSObjectType()) {
         when 'Contact' {
           return [SELECT FirstName FROM Contact WHERE Id = :this.whoId][0].FirstName;
         }
         when 'Lead' {
           return [SELECT FirstName FROM Lead WHERE Id = :this.whoId][0].FirstName;
         }
         when else {return 'Valued Customer';}
       }
     }
     else return 'Valued Customer';
    }
}
5
  • Thanks, @cropredy. Nov 6 '19 at 8:20
  • I am getting the following error while adding the vf component in email template: Error occurred trying to load the template for preview: Attempt to de-reference a null object. Please try editing your markup to correct the problem. Please help. Also, i did not understand the usage of value="{!firstName}"? Nov 11 '19 at 15:07
  • sorry - fixed. works fine now. {!firstName} implicily invokes the getFirstName method in the component controller. SFDC/VF looks for a getter method on the merge field
    – cropredy
    Nov 11 '19 at 23:11
  • Got it. Thanks. Nov 12 '19 at 11:57
  • Works perfectly. Nov 12 '19 at 19:59
0

please check the recipient type for your email template and replace your template as below :

    <p>Dear {!recipient.FirstName},</p>
    <p>You have been invited to this event: {!relatedTo.Subject}.</p>
   <p>You can access the event at <a href = 
                "https://b2b-dev1--inwidev1.lightning.force.com/{!relatedTo.id}">{!relatedTo.Subject}
            </a> </p>
    <p>Ref: {!recipient.Id}.</p>
    </body>
</html>

8
  • Please do not ask for votes or best answer on SFSE.
    – David Reed
    Nov 4 '19 at 16:33
  • Hi Soniya, Thanks for your response. I get the error: "Error: Syntax error. Found '<' ", when I use the following code in VF ET: Nov 4 '19 at 17:06
  • If I copy your code, I receive the following errors: Error Error: Lead_Event_Notification_Template_Saibal line 10, column 57: The element type "body" must be terminated by the matching end-tag "</body>" Error Error: The element type "body" must be terminated by the matching end-tag "</body>" Nov 4 '19 at 17:13
  • Hi David, character limit on comments is quite stifling. Nov 4 '19 at 17:17
  • Hi Soniya, for your reference: <messaging:emailTemplate recipientType="Lead" relatedToType="Event" Nov 4 '19 at 17:22
0

This one works too.

Dear Customer,

You have been invited to this event: {!relatedTo.Subject}.

Start Time- {!relatedTo.StartDateTime}

End Time- {!relatedTo.EndDateTime}

Duration (in minutes)- {!relatedTo.DurationInMinutes}

Ref: {!relatedTo.WhatId}

Thanks,
Corporate Sales Team.

2
  • more of a workaround as you've avoided fetching the firstname and just calling everyone Customer
    – cropredy
    Nov 4 '19 at 22:22
  • 95% achieved through my approach. We must keep in mind about Salesforce limitations. Any tips on how to fetch the Lead First Name? Please let me know. Nov 5 '19 at 8:15
0

This code worked:

Dear Customer,

You have been invited to this event: {!relatedTo.Subject}.

You can access the event at https://b2b-dev1--inwidev1.lightning.force.com/{!relatedTo.id}">{!relatedTo.Subject}

Ref: {!relatedTo.WhoId}

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.