SendEmail
SendEmail copied to clipboard
TreatTargetObjectAsRecipient = false isn't working
Hi Dan,
Love this and actively use it but came across a bug. I was able to use your code to fix it so I wanted to share!
Problem: Salesforce defaults TreatTargetObjectAsRecipient to true and while this app provides an option to set it to false; it's not set anywhere in the code so the Contact associated continues to be a recipient even if you configure it false in process builder.
Solution: Starting at line 285; I added an else condition to set the value to false when appropriate:
if (request.TreatTargetObjectAsRecipient == true) {
// Make sure we have an email
if (String.isBlanK(contactOrLeadEmailsById.get(request.ContactOrLeadID))) {
// Create error no email
System.debug('### No Contact or Lead email address could be found with the Id >>> '+request.ContactOrLeadID);
throw new SendEmailException('No Contact or Lead email address could be found with the Id: '+request.ContactOrLeadID);
} else mail.setTreatTargetObjectasRecipient(true);
} else {
// Setting the TargetObjectAsRecipient to false ensures the Contact/Lead will not receive this email
mail.setTreatTargetObjectAsRecipient(false);
}
Use Case: This is a great way to have a custom email template and get the context to populate merge fields and not email the Contact... Salesforce insists on having a contact but this suppresses the email to them and opens the door wide-open to use custom templates; get that context from the Account/Contact for internal communications and have the Contact be a recipient of the message.
Great app--thanks for sharing and hope my small contribution helps!
-Dan
@dan-boudreau thank you for the fix - I noticed the same thing. @danhowellnz is there any plan to get this into the managed package?
should have checked this section, we went live and noticed the a bug due to this parameter not being set "false". 🤦
we are changing this logic around to default to yes if that flag isn't set but there is contactOrLead set and it's valid/has an email.
Code has been updated in this repo, managed package getting updated soon. Do you want to test with the code or wait until the package is available? @pmk-tyro @adwhite75 @dan-boudreau