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
Southern gal developerSouthern gal developer 

How can I send object id to Email Template from MassEmailMessage during Apex Trigger

I have created an Email Templete using object fields in the body of the email.

I have a trigger which occurs after the insert of a single record to a custom object I created. After the insert, I would like to send an email to a list of user. 

I found examples of how to do this but. . .how do you send the id of the record you would like to use when using the template with assigned object fields.

This is my code:

trigger WriteNotifications on CustomTable__c (After insert) {

  //This is not the actual select but gives an idea of how I am getting my data
//Declare variable
ID custom_ID;

for (CustomTable__c C_master : Trigger.new) {
        custom_id = C_master.Id;
    }

//Get the list of user ids
    List<User> lstUsed = [select id, name FROM USER where name =: 'Smith'];
   
  List<String> sendTo = new List<String>();

  for (User uses : lstUsed ){
       sendTo.add(uses.Id);
    }

 EmailTemplate et = [Select id from EmailTemplate where name = 'My Template' limit 1];

    Messaging.MassEmailMessage mail = new Messaging.MassEmail Message();
    mail.setTargetObjectIds(sendTo);
    mail.setSenderDisplayName('Sending Person');
    mail.setTemplateId(et.id);
    Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}
How can pass the custom_ID value to the  Email Temple to reference the record from my custom table?
Raj VakatiRaj Vakati
try like this
 
trigger WriteNotifications on CustomTable__c (After insert) {

  //This is not the actual select but gives an idea of how I am getting my data
//Declare variable
ID custom_ID;

for (CustomTable__c C_master : Trigger.new) {
        custom_id = C_master.Id;
    }

//Get the list of user ids
    List<User> lstUsed = [select id, name FROM USER where name =: 'Smith'];
   
  List<String> sendTo = new List<String>();

  for (User uses : lstUsed ){
       sendTo.add(uses.Id);
    }

		EmailTemplate et = [Select id from EmailTemplate where name = 'My Template' limit 1];
List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();


	
for (CustomTable__c C_master : Trigger.new) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

	mail.setTargetObjectId(C_master.Id);
	mail.setTemplateId(et.Id);
    Messaging.MassEmailMessage mail = new Messaging.MassEmail Message();
    mail.setTargetObjectIds(sendTo);
    mail.setSenderDisplayName('Sending Person');
    mail.setTemplateId(et.id);
	mailList.add(mail);

//    Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}
Messaging.sendEmail(mailList);


}

 
Southern gal developerSouthern gal developer
Thank you for the response - I will try the code now.
 
Southern gal developerSouthern gal developer
I am getting this error:
Error: Compile Error: Duplicate variable: mail at line 30 column 36
Biswojeet Ray 11Biswojeet Ray 11
Hi,
Please try this
trigger WriteNotifications on CustomTable__c (After insert) {

  //This is not the actual select but gives an idea of how I am getting my data
//Declare variable
ID custom_ID;

for (CustomTable__c C_master : Trigger.new) {
        custom_id = C_master.Id;
    }

//Get the list of user ids
    List<User> lstUsed = [select id, name FROM USER where name =: 'Smith'];
   
  List<String> sendTo = new List<String>();

  for (User uses : lstUsed ){
       sendTo.add(uses.Id);
    }

		EmailTemplate et = [Select id from EmailTemplate where name = 'My Template' limit 1];
List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();


	
for (CustomTable__c C_master : Trigger.new) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

	mail.setTargetObjectId(C_master.Id);
	mail.setTemplateId(et.Id);
    Messaging.MassEmailMessage massmail = new Messaging.MassEmailMessage();
    massmail.setTargetObjectIds(sendTo);
    massmail.setSenderDisplayName('Sending Person');
    massmail.setTemplateId(et.id);
	mailList.add(massmail);

//    Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}
Messaging.sendEmail(mailList);
}


Kindly let me know if it helps you and please mark as Best Answer.

Thanks and Regards,
Biswojeet
Southern gal developerSouthern gal developer
Getting this Compile Error:
Error: Compile Error: Method does not exist or incorrect signature: void add(Messaging.MassEmailMessage) from the type List<Messaging.SingleEmailMessage> at line 34 column 14

Also. . .the mail variable is set to values but never passed to an object. How does the mail variable get used?
Biswojeet Ray 11Biswojeet Ray 11

Hi southern,

Please try this.

 

trigger WriteNotifications on CustomTable__c (After insert) {

  //This is not the actual select but gives an idea of how I am getting my data
//Declare variable
ID custom_ID;

for (CustomTable__c C_master : Trigger.new) {
        custom_id = C_master.Id;
    }
string setTargetObjectIdValue = [select id, name FROM USER where name =: 'Smith' LIMIT 1].Id;

		EmailTemplate et = [Select id from EmailTemplate where name = 'My Template' limit 1];
//List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();


	
for (CustomTable__c C_master : Trigger.new) {
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
	mail.setWhatId(C_master.Id);
	mail.setTemplateId(et.Id);
    mail.setTargetObjectId(setTargetObjectIdValue);
    mail.setSenderDisplayName('Sending Person');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
//Messaging.sendEmail(mailList);
}

Kindly let me know if it helps you and please mark as Best Answer.

Thanks and Regards,
Biswojeet
Southern gal developerSouthern gal developer
Thank you for the quick response - I will try it and post results 

Thank you again !!
Southern gal developerSouthern gal developer
This is what I ended up with :
I ran into another issue where when I adding a value to the setWhatId  it was a custom object id. This is usually reserved for contact, users, etc but I found an article where there was a work around.

 
    
    // Pick a dummy Contact
    Contact c = [select id, Email from Contact where email <> null limit 1];

    // Construct the list of emails we want to send
    List<Messaging.SingleEmailMessage> lstMsgs = new List<Messaging.SingleEmailMessage>();

    Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
    msg.setTemplateId([Select id from EmailTemplate where name = 'My Template' limit 1].id);
    msg.setWhatId(pasms_edob_id);
    msg.setTargetObjectId(custom_id);
    msg.setToAddresses(sendTo);

    lstMsgs.add(msg);

    // Send the emails in a transaction, then roll it back
    Savepoint sp = Database.setSavepoint();
    Messaging.sendEmail(lstMsgs);
    Database.rollback(sp);

    // For each SingleEmailMessage that was just populated by the sendEmail() method, copy its
    // contents to a new SingleEmailMessage. Then send those new messages.
    List<Messaging.SingleEmailMessage> lstMsgsToSend = new List<Messaging.SingleEmailMessage>();
    
    for (Messaging.SingleEmailMessage email : lstMsgs) {
    Messaging.SingleEmailMessage emailToSend = new Messaging.SingleEmailMessage();
    emailToSend.setToAddresses(email.getToAddresses());
    emailToSend.setPlainTextBody(email.getPlainTextBody());
    emailToSend.setHTMLBody(email.getHTMLBody());
    emailToSend.setSubject(email.getSubject());
    lstMsgsToSend.add(emailToSend);
    }
    Messaging.sendEmail(lstMsgsToSend);


This sent the email using a custom object id !!

Here is the link to the answer I found : https://opfocus.com/sending-emails-in-salesforce-to-non-contacts-using-apex/

Thanks for all of the great answers.