Handling Org-Wide Email Addresses
Handles the issue that OWEAs cannot be created in scratch orgs prior to deployment
OWEA's cannot be created in Scratch Orgs prior to deployment. The following error occurs when Email Alerts are pushed to a Scratch Org without a verified OrgWideEmailAddress.
Email Alerts need a verified OrgWideEmailAddress
Handle all emails that would use an OWEA at the Apex level.
- Create an apex class that queries for the OWEA desired.
- If found -> use it as the from address.
- If not found (e.g. scratch org) -> use the current user’s email address as the from address
- Add the InvocableMethod decorator to enable calling from flows
Example
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
OrgWideEmailAddress[] owea = [select Id from OrgWideEmailAddress where Address = '[email protected]'];
if ( owea.size() > 0 ) {
mail.setOrgWideEmailAddressId(owea.get(0).Id);
}
mail.toAddresses = new String[] { <contact ID> };
mail.subject = 'OWEA Test Message';
mail.plainTextBody = 'This is the message body.';
Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage> {mail};
Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
Last modified 1mo ago