function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
WEN JIEWEN JIE 

Can't get value from Merge field when use Email Templete

Hi,

 

I have a custom object as "A__c". And I create a trigger when I update this object record. This trigger will send mail to some users.

 

Then I create an email visualforce template. And let my trigger invoke this template when I send mail.

But I met a problem about the template.

 

I set the recipientType as "User", and relatedToType as my custom object "A__c"

 

<messaging:emailTemplate subject="Assign Audit" recipientType="User" relatedToType="A__c">
<messaging:plainTextEmailBody >
Hello {!recipient.name}

This is {!relatedTo.Name}

</messaging:plainTextEmailBody>
</messaging:emailTemplate>

When I use "Send Test and Verify Merge Fileds",assign an user and an "A" record, I can see the corresponding value display on this mail body.

 

But if I update a record then the trigger run and invoke this template, the {!recipient.name} can get value and display, but {!relatedTo.Name} can't get any value.

 

 

So does anyone meet this problem before? Or whether I have some wrong code at this template.

 

Thank you!

Best Answer chosen by Admin (Salesforce Developers) 
WEN JIEWEN JIE

I understand what problem with my template.

I need to set the record id for my merge field in that email template.

In my trigger :

 

mail.setTemplateID('00X90000001EU06');
mail.setTargetObjectId(User.Id);
mail.setSaveAsActivity(false);
mail.setWhatId(a.Id);

 And a is my A__c instance.

Then my template can get the corresponding values