Hi Can you please debug and fix this trigger.

When I send a mail through trigger merge fields are not picking automatically. Its showing empty.

      trigger SendEmailNotificationtoCandidate on Ts__c (after update) {

      Ts__c ts = trigger.new[0]; 
      String[] toAddresses = new String[] {ts.Candidate_Email__c}; 

      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

      mail.setSenderDisplayName('Test Team');

     if (Trigger.isUpdate) { 
        if(ts.Status__c == 'Submitted') {
              EmailTemplate et=[
                              SELECT id,Body,DeveloperName,Name,Subject 
                              FROM EmailTemplate 
                              WHERE DeveloperName=:'Ts_Submitted_for_Contact'

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



Please fix it as soon as possible.

Thanks in advance.


That depends on what merge fields you are trying to get in the template. If they are not from the user record then your merge fields will come out blank (no record reference).

According to SFDC documentation: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

you can set the WhatId to help with field merging but only if you've passed a contact reference in the targetObjectId.

Not sure if that answers your question. If not then please provide more information about your template such as who the recipient is (e.g. user, contact, lead), which object/record are you trying to merge with the template.

| improve this answer | |
  • Hi Danny, Thanks for your reply. Here is the template. kindly check once – Manoj Dega Mar 28 '16 at 15:03
  • 1
    So Ts__c is a custom object so you'd need to specify that in the WhatId. Only problem is you can't do that if the targetID is a user. Target ID has to be a contact. You won't be able to use the template here as far as I know. – Danny Singh Mar 28 '16 at 15:16

I got the solution for this. Just added these.

| improve this answer | |

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.