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
tirantiran 

Problem about Email Template

  Hello,everyone.I am new for the salesforce. Now i have a problem about sending email.

 

  I want to send outbound Emails to the clients if the status is update.However,i also create a personal HTML Email template.

 

  Now,the problem is,if i just send a text email that i write in the apex class,it runs well,i can receive the email.but if i use the email template,it always has a error .Please help me ,thank you very much!

 

 

 The code trigger:


trigger SendEmail on Opportunity (after update) {
String OldStatutPolice;
String NewStatutPolice;
Opportunity o=Trigger.New[0];
OldStatutPolice=trigger.old[0].Statut_Police__c;
NewStatutPolice=trigger.new[0].Statut_Police__c;


if((OldStatutPolice=='Proposition') && (NewStatutPolice=='Nouvelle Police (active)') )
{
NewPoliceSendEmail.SendEmail(o);
}
}

 

The code Apex Class:

 

public with sharing class NewPoliceSendEmail {


public static void SendEmail(Opportunity o)
{
      
 
if(o.Statut_Police__c=='Nouvelle Police (active)'){
    
    
    Opportunity OppAcc = [select Accountid from Opportunity where id = :o.id];
          
     Contact contact = [select Email from Contact where Accountid = :OppAcc.AccountId and Destinataire__c='Oui'];
    EmailTemplate et = [SELECT Id FROM EmailTemplate WHERE DeveloperName='Mail_Remerciement'];
 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
 
                        mail.setTargetObjectId(contact.id);

                       mail.setBccSender(false);
                       mail.setSenderDisplayName('xxxxxx@xxxxxxl.com');
                       mail.setSubject('Merci');    
                       mail.setTemplateId(et.Id);
                       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}

 

  The error is INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body, html body, subject and charset may not be specified : []:

Best Answer chosen by Admin (Salesforce Developers) 
Navatar_DbSupNavatar_DbSup

Hi,

 

When sending the email from Messaging.SingleEmailMessage and setting the email template. There no need to set the subject of the email .so remove the “mail.setSubject('Merci'); ” from your code.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

All Answers

Navatar_DbSupNavatar_DbSup

Hi,

 

When sending the email from Messaging.SingleEmailMessage and setting the email template. There no need to set the subject of the email .so remove the “mail.setSubject('Merci'); ” from your code.

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

 

 

This was selected as the best answer
tirantiran

It runs very well!!thanks!!

sindhu surusindhu suru
I have related problem, When we use setTemplateId with mass email, the merge fields are not showing up. How to pass a record ID to the email template. Please help.