Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Salesforce by (11.9k points)

i have to send an email to a user in salesforce using email template.this template contain merge field type of custom object.

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTargetObjectId(user.get(0).id);
mail.setTargetObjectId(user.get(0).Id)
mail.setTemplateId(specifier.get(0).Template_id__c);
mail.saveAsActivity = false;
mail.setWhatId(custom_object.Id);

i read in documentation If you specify a contact for the targetObjectId field, you can specify a whatId as well. This helps to further ensure that merge fields in the template contain the correct data. The value must be one of the following types: Account Asset Campaign Case Contract Opportunity Order Product Solution Custom

but if we are sending email to a user not to contact then how to assign a custom object for merge field type in custom objects as in the above code

1 Answer

0 votes
by (32.1k points)

Not exactly sure if this is possible as it depends on the relationships between your custom object and your users. In a custom formula field on your User object use the following code:

TEXT(CustomObject__r.CustomField__c)

Then your template can be changed into a User template and the merge fields would be the formula that actually pointed to your custom object instance.

To learn in-depth about Workflow in Salesforce, sign up for an industry based Salesforce training online.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94.1k users

Browse Categories

...