+ Start a Discussion
FastSnailFastSnail 

How to access user and user. contact fields in a Visualforce Email template through ?

Hello,
We want to create a VF Email template that access the owner of a record, even in fact the contact associated to this user, as this is a Portal user. I can't get it to work. It is not done through RecipientType but through relatedToType
<messaging:emailTemplate relatedToType="MySObject__c" subject="the subject is ......
{!relatedTo.Owner.name} works fine in the VF template.
But:
- {!relatedTo.Owner.CompanyName} and all user.CustomFields__c return 'Error: Invalid field Company for SObject Name.'
- {!relatedTo.owner.contact} does not work either.
Thanks in advance for your help, A workaround is appreciated.
Jerome
Maharajan CMaharajan C
Hi,

You have to add a recipientType="User" in your header like below to access the User Fields:

<messaging:emailTemplate subject="this is the subject" recipientType="User" relatedToType="Lead">
<messaging:plaintextEmailBody >

Full Name            : {!RelatedTo.LastName}
Annual Revenue:  {!RelatedTo.AnnualRevenue}
Lead Owner         : {!RelatedTo.Owner}
Owner Company  : {!RelatedTo.Owner.CompanyName}

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

Steps for Visualforce Template

NewEmailTemplate->VisualforceTemplate->Enter the Details(Recipient Type -USER , Related To Type -None)->Edit Template ->Copy and paste the Above Code->Send and Test verify merge fields->Choose User(Recipient Type) , Chose Lead Record(Related To Type)->Send Email to View->Ok

Use the Above Steps to get the Proper Output.

Reference Link: https://help.salesforce.com/articleView?id=000175913&language=en_US&type=1

Let me know if it works or not!!!

If it works mark this as a best answer!!!

Thanks,
​Raj
Maharajan CMaharajan C
To access the user fields you have to do like what i mentioned
FastSnailFastSnail
Thank you for the quick reply Maharaja,
But as I stated in my quetion, the goal is NOT to access the data through recipientType="User", but through relatedToType="MySObject__c".
We need to access Sobjzct record.Owner.contact.name, the owner being a Customer portal user.
Can we do this?
Thanks in advance for your support. Have a good weekend,
Jerome