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
ssousanssousan 

Can i use a field from a custom object in an Email Templates?

I'm writing an Email template that includes merged fields,

 

When I add an account field it works fine,

 

But when I try to add a field from a custom object it does not work,

 

Do Email templates accept fields from custom objects?

 

Thanks  

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setTargetObjectId(<contact-id>);
message.setWhatId(<custom-object-id>);
message.setTemplateId(<email-template-id>);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { message });

Note that, as was said earlier, sending emails to users doesn't allow sending by template. Instead, you should probably create a contact record for each user, then send the email to the contact instead.

All Answers

sfdcfoxsfdcfox
When using custom objects, Text, HTML, and Custom templates must be sent with the "Related To" set to the custom object record. You have to go to the specific custom object record, and use the Activity History related list's Send Email button, or select the custom record in the Related To box using the magnifying glass icon.

If you're doing this in Apex Code, the same basic rule applies; you have to set the What Id for the email message (via EmailMessage.setWhatId) to the custom object's record ID.

Visualforce email templates can refer to indirectly related records.
MoggyMoggy
If you do that out side apex you will need to use the API name of the custom Object to get the field
myCustomObject__c.myFieldName__c or
myCustomObject__c.Link ( for the detail link to that custom record)
ssousanssousan

This is what i used:

 

myCustomObject__c.myFieldName__c

 

And when i send the email i get blank text in that location

 

Best

ssousanssousan

So this is how i set it up:

 

Inquery__c inquery = trigger.new[0];

String[] toAddresses = new String[] {inquery.email__c};
  Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  mail.setTargetObjectId(inquery.OwnerID);
  mail.setWhatId(inquery.OwnerID);

  mail.setSenderDisplayName('Salesforce Support');
  mail.setUseSignature(false);
  mail.setBccSender(false);
  mail.setSaveAsActivity(false);

 EmailTemplate et=[Select id from EmailTemplate where DeveloperName=:'Invitation_to_register_for_Training'];
 mail.setTemplateId(et.id);
 Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

 

but im getting the following error:

 

Error:Apex trigger send_notification caused an unexpected exception, contact your administrator: send_notification: execution of AfterUpdate caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_ID_FIELD, WhatId is not available for sending emails to UserIds.: []: Trigger.send_notification:

 

Do you know why?

 

Thanks

 

MoggyMoggy

What Id only works for contacts not for user, comment it out, and it should work

ssousanssousan

Yes i just added that line of code,

 

Because i was trying to follow  instructions,

 

But i guess im not doing the correct thing,

 

Best 

MoggyMoggy

is that a single or a multiple email just wondering as you have [] after the new command

ssousanssousan

Its a single Email

MoggyMoggy

Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

I have in my codes always only
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

Messaging.sendEmail(mail);

 

MoggyMoggy
and if you do that for more then one you will need to add every single mail item one by one to the array
ssousanssousan

I get an error when i write:

 

Messaging.sendEmail(mail);

 

that says:

 

Error: Compile Error: Method does not exist or incorrect signature: Messaging.sendEmail(Messaging.SingleEmailMessage) at :

 

But the way i have written it works fine.

sfdcfoxsfdcfox

You need to have a List<Messaging.SingleEmailMessage>, not just a single message.

ssousanssousan

Not sure exactly how to do that,

Could you give me an example,

 

Thanks

sfdcfoxsfdcfox
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
message.setTargetObjectId(<contact-id>);
message.setWhatId(<custom-object-id>);
message.setTemplateId(<email-template-id>);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { message });

Note that, as was said earlier, sending emails to users doesn't allow sending by template. Instead, you should probably create a contact record for each user, then send the email to the contact instead.

This was selected as the best answer