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
Azeez ThummalapalliAzeez Thummalapalli 

which is best approach for custom email alerts, Declarative vs Programmatic?

Object_A and Object_B are two related(Lookup) objects in Salesforce, where A is parent and B is child. Requirement is, when a new Object_B record is created, an email alert should be sent to Object_B owner(not necessarily who created, because we are setting the owner in code), to parent Object_A record Owner, and to one who created parent Object_A record. Note that these owners can be Queues or users, and they can be same for both the records(i.e. object_A and Object_B records may or may not have same owners). Note that, no one should receive duplicate email alerts for the same object_B record.

For the above scenario, to send email alerts, we can use either Workflow email alerts or Custom Code to send emails. Below I am providing my opinions on both the approaches. I believe, in this case custom code makes more sense than declarative approach. Please correct me if I am wrong, by looking at the below explanation.

Workflow/ProcessBuilder Email alerts
  • I may need to create multiple workflows to make sure that duplicate email alerts are not sent. Of course, we can use Process Builder in this case. But still if the owners are Queues and a few users are part of both of those two queues, then it will be difficult ensure that duplicate emails are not sent.
  • The limit for Email alerts via workflows is 1000 per licence per day. So the email alerts counting towards the governor limits.

Custom Code using Messaging.SingleEmailMessage
  • Define a new static method in a helper class, and write logic to send email using Messaging.SingleEmailMessage, and in code we can make sure that duplicate email alerts are not sent to same user for same Object_B record. Call this method from After Trigger.
  • Though there are governor limits, sending emails to internal users will not be counted towards the governor limits. I believe, this is the best because there is no chance to get governor limit exception in this case.
Is my analysis correct? Is there any other best approach?