-2

I'am trying to send email template to the user.Email template isnot sending to that user.

enter image description here

 public class s360_InquiryUpdateDetails {
 public static void inquiryInformation(List < Opportunity > opList) {
set < id > primaryAccount = new set < id > ();
set < id > secondaryAccount = new set < id > ();
set < id > thirdAccount = new set < id > ();
map < id, string > primaryOppName = new map < id, string > ();
map < id, string > secondaryOppName = new map < id, string > ();
map < id, string > ThirdOppName = new map < id, string > ();

for (Opportunity op: opList) {
 //Primary Account
 if (op.AccountId != null) {
  primaryAccount.add(op.AccountId);
  primaryOppName.put(op.AccountId, op.Name);
 }
 //secondary
 if (op.Secondary_Inquiring_Company__c != null) {
  secondaryAccount.add(op.Secondary_Inquiring_Company__c);
  secondaryOppName.put(op.Secondary_Inquiring_Company__c, op.name);
 }
 //Third
 if (op.Third_Inquiring_Company__c != null) {
  thirdAccount.add(op.Third_Inquiring_Company__c);
  ThirdOppName.put(op.Third_Inquiring_Company__c, op.name);
 }
} // for loop en
map < id, id > primaryAccountMap = new map < id, id > ();
map < id, string > primaryAccountMap1 = new map < id, string > ();
map < id, id > secondaryAccountMap = new map < id, id > ();
map < id, string > secondaryAccountMap1 = new map < id, string > ();
map < id, id > thirdAccountMap = new map < id, id > ();
map < id, string > thirdAccountMap1 = new map < id, string > ();

for (Account ac: [SELECT id, name, Key_Account__c, Key_Account_Responsibility__c from Account where(id in : primaryAccount OR id in : secondaryAccount OR id in : thirdAccount)
  and Key_Account__c = true
 ]) {

 if (primaryAccount.contains(ac.Id)) {
  primaryAccountMap.put(ac.id, ac.Key_Account_Responsibility__c);
  primaryAccountMap1.put(ac.id, ac.name);



 } else if (secondaryAccount.contains(ac.Id)) {
  secondaryAccountMap.put(ac.id, ac.Key_Account_Responsibility__c);
  secondaryAccountMap1.put(ac.id, ac.name);
 } else if (thirdAccount.contains(ac.Id)) {
  thirdAccountMap.put(ac.id, ac.Key_Account_Responsibility__c);
  thirdAccountMap1.put(ac.id, ac.name);
 }

}
List < Messaging.SingleEmailMessage > mails = new List < Messaging.SingleEmailMessage > ();
for (Opportunity op: opList) {
 if (primaryAccountMap.containsKey(op.AccountId)) {
  mails.add(createEmailMessage(primaryAccountMap.get(op.AccountId), primaryAccountMap1.get(op.AccountId), primaryOppName.get(op.AccountId)));
 }
 if (secondaryAccountMap.containsKey(op.AccountId) && !primaryAccountMap.containsKey(op.AccountId)) {
  mails.add(createEmailMessage(secondaryAccountMap.get(op.Secondary_Inquiring_Company__c), secondaryAccountMap1.get(op.AccountId), secondaryOppName.get(op.AccountId)));

 }
 if (thirdAccountMap.containsKey(op.AccountId) && !primaryAccountMap.containsKey(op.AccountId) && !secondaryAccountMap.containsKey(op.AccountId)) {
  mails.add(createEmailMessage(thirdAccountMap.get(op.Third_Inquiring_Company__c), thirdAccountMap1.get(op.AccountId), ThirdOppName.get(op.AccountId)));

 }
}
system.debug('mails####' + mails);
Messaging.sendEmail(mails);
}

private static Messaging.SingleEmailMessage createEmailMessage(String 
     userId, 
    string name, string OppName) {
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    EmailTemplate emailTemplate = [Select Id, Name, HtmlValue, Body, Subject 
    from EmailTemplate where DeveloperName = 'Global_Key_Account_Update'];
    String subject = emailTemplate.Subject;
    subject = subject.replace('{!Opportunity.Name}', OppName);
    subject = subject.replace('{!Account.Name}', name);
    String htmlBody = emailTemplate.HtmlValue;
    htmlBody = htmlBody.replace(' {!Account.Key_Account_Responsibility__c} ', userId);
    htmlBody = htmlBody.replace('{!Opportunity.Name}', OppName);
    mail.setTemplateId(emailTemplate.Id);
    mail.setTargetObjectId(userId);
    mail.saveAsActivity = false;
    //mail.setReplyTo('annappaph2@gmail.com');
    mail.setSenderDisplayName('Test Mail');
    mail.setSubject('shawocr');
    String body = 'Test Mail';
    mail.setHtmlBody(body);

    return mail;
   }

  }

I have updated following code,but still itsnot working!!!!

 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  EmailTemplate emailTemplate = [Select Id, Name, HtmlValue, Body, Subject from EmailTemplate where DeveloperName = 'Global_Key_Account_Update'];
  String subject = emailTemplate.Subject;
  subject = subject.replace('{!Opportunity.Name}', OppName);
  subject = subject.replace('{!Account.Name}', name);
  String htmlBody = emailTemplate.HtmlValue;
  htmlBody = htmlBody.replace(' {!Account.Key_Account_Responsibility__c} ', userId);
  htmlBody = htmlBody.replace('{!Opportunity.Name}', OppName);
   String plainBody = emailTemplate.Body;
   plainBody = plainBody.replace('{!Account.Key_Account_Responsibility__c}', userId);
  plainBody = plainBody.replace('{!Opportunity.Name}',OppName);
   mail.setHtmlBody(htmlBody);          
   mail.setPlainTextBody(plainBody);           
  mail.setTemplateId(emailTemplate.Id);
  mail.setTreatBodiesAsTemplate(True);
  mail.setTargetObjectId(userId);
  mail.saveAsActivity = false;
  //mail.setReplyTo('annappaph2@gmail.com');
  mail.setSenderDisplayName('Test Mail');
  mail.setSubject('shawocr');
  String body = 'Test Mail';
  mail.setHtmlBody(body);
  • Are you getting any exception ..? Check your debug log.. – Ayub Jun 26 '18 at 10:20
  • iam not getting any exception.iam getting mail as well.but iam not getting email template in mail – SFDC FAN Jun 26 '18 at 10:27
1

I think you can specify either setHtmlBody or setTemplateId not both together.

As per doc, only setHtmlBody and setPlainTextBody can be used together otherwise you should specify only one. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

setHtmlBody(htmlBody)

Optional. The HTML version of the email, specified by the sender. The value is encoded according to the specification associated with the organization. You must specify a value for setTemplateId, setHtmlBody, or setPlainTextBody. Or, you can define both setHtmlBody and setPlainTextBody.

Here is the related question if you want to create a template in apex: How does setTreatBodiesAsTemplate(treatAsTemplate) work?

  • I dont think thats the issue.Please check my updated code – SFDC FAN Jun 26 '18 at 10:55
  • Let me check ... – Ayub Jun 26 '18 at 11:04
  • I can see you are setting mail.setHtmlBody(htmlBody); and as well as mail.setTemplateId(emailTemplate.Id); .... also mail.setHtmlBody(htmlBody) is written two times.... comment mail.setTemplateId(emailTemplate.Id); and give a try – Ayub Jun 26 '18 at 11:06

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.