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
cjencjen 

Trigger to send an email to a list of contacts

HI, looking for some help to send an email. We need to send an email to a list of contacts where Role__c (custom) = Primary when an Asset related to the same Account moves to Status = Implementation Complete.

1. how can i get the list of contacts to send to?
2. How can i identify the template to use?
3. What is the best way to trigger the email? Can it be done with process builder?

the list of contacts should not exceed 10, but need to send the email to all Primary contacts related to the same account the asset is related to.

Any help would be much appreciated! Thanks!
NagendraNagendra (Salesforce Developers) 
Hi,

Yes, you can send email to more than 10 contacts from the apex.

Please find the sample code below and tweak it as per your requirement which helps you in resolving the issue.
EmailTemplate templateId = [Select id from EmailTemplate where name = 'Your template Name'];
List<Messaging.SingleEmailMessage> allmsg = new List<Messaging.SingleEmailMessage>();
for(contact con : contactList)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setTemplateID(templateId.Id); 
mail.setTargetObjectId(con>id)
mail.setSaveAsActivity(false);
allmsg.add(mail);
}
Messaging.sendEmail(allmsg,false);
Basically, add all contact in the list and then send them in a single shot. In one time you can send 100 emails. So this will solve your problem.

Hope this helps.

Please mark this as solved if it's resolved so that it gets removed from the unanswered queue which results in helping others who are encountering a similar issue.

Thanks,
Nagendra


 
cjencjen
@Nagendra , first of all thank you so much for responding. However,  i am a non developer. Do i put this code in an apex class?  Can I activate this class from Process Builder when the asset criteria is met? How do i dynamically get the list of contacts? It will be a list of contacts where role__c = primary and contact.account.id = asset.account.id , so i am not sure how to just add them. Sorry for the lack of knowledge, but certainly appreciate any help, thanks again!