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
Eric PohlabelEric Pohlabel 

Set email alert to record owner

I have an email alert that currently sends from the current users address.  I know that I can change this to an Org-Wide default - but is there a way to dynamically change it to the record owner?

We are sending email alerts from Cases via a process flow.  The user who is kicking off the flow is not necessarily the case owner BUT I want the email that is sent to be sent from the case owner such that the case owner's email address shows in the "From" line and replies to the email go that address.
Alain CabonAlain Cabon
Sending email alerts from Cases via a process flow.

1) invocable method ( some Apex code passing the email of the case owner to the method )

A complete sample:
https://www.linkedin.com/pulse/sending-email-attachment-using-process-builder-shweta-soparkar-

but it lacks the setSenderDisplayName(displayName) in the example above with an important constraint (setOrgWideEmailAddressId).

2) setSenderDisplayName(displayName) Optional. The name that appears on the From line of the email. This cannot be set if the object associated with a setOrgWideEmailAddressId for a SingleEmailMessage has defined its DisplayName field.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_base.htm?search_text=setSenderDisplayName

User-added image

 
public class sendAnEmail
{
    @InvocableMethod(label='Send an email from apex class' description='sends an email')
    public static void sendEmailWithAttachment(List<String> caseowneremails)
    {
         for (String caseowneremail:caseowneremails) {
               
               Messaging.SingleEmailMessage semail= new Messaging.SingleEmailMessage();
               semail.setSubject('Quote Issued');
               String[] emailIds= new String[]{'**********@gmail.com'};
               semail.setToAddresses(emailIds);
               semail.setSenderDisplayName(caseowneremail);
               semail.setPlainTextBody('Please find an important message');
               Messaging.sendEmail(new Messaging.SingleEmailMessage[]{semail});
          }          
    }
}

Regards