You need to sign in to do that
Don't have an account?
Southern 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?
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?
Error: Compile Error: Duplicate variable: mail at line 30 column 36
Please try this
Kindly let me know if it helps you and please mark as Best Answer.
Thanks and Regards,
Biswojeet
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?
Hi southern,
Please try this.
Kindly let me know if it helps you and please mark as Best Answer.
Thanks and Regards,
Biswojeet
Thank you again !!
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.