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
Satya Ranjan MohantySatya Ranjan Mohanty 

How to send email from record owner

Hi ,

I need help how to send the email  from the record owner name and email adress  
   code i have used 
              Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment});
                message.setSubject(e.Email_Subject__c);
                message.setPlainTextBody(e.Email_Body__c);
                message.setSenderDisplayName(users[0].Name);
                message.setReplyTo(users[0].Email);
                String[] emailRecipients = e.Email_Recipients__c.split(',');
                message.setToAddresses(emailRecipients);
                mails.add(message);
Here is the issue is owner name is populating but email address  is showing off who has scheduled the Batch Apex job 
may i know where i am doing the mistake ,let me know if you having any query 

thankx
satya
Raj VakatiRaj Vakati
Change is below and try . e.Owner.Email is the record owner email and e.Owner.Name is the records  name
 
Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
                message.setFileAttachments(new Messaging.EmailFileAttachment[] {attachment});
                message.setSubject(e.Email_Subject__c);
                message.setPlainTextBody(e.Email_Body__c);
                message.setSenderDisplayName(e.Owner.Name);
                message.setReplyTo(e.Owner.Email);
                String[] emailRecipients = e.Email_Recipients__c.split(',');
                message.setToAddresses(emailRecipients);
                mails.add(message);

 
Satya Ranjan MohantySatya Ranjan Mohanty
Hi Raj,
Thank you fro your quick responce .

After doing the change as you said it is throwing the below error

Failed to invoke future method 'public static void DoExport()' on class 'ReportExporter' for job id '7072D00000B5U5X'
 
caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Report_Exporter__c.Owner
 
Class.ReportExporter.DoExport: line 66, column 1
 
line no.is ---message.setSenderDisplayName(e.Owner.Name);

Regards,
satya
Satya Ranjan MohantySatya Ranjan Mohanty
Raj,
Here is the query what i am doing in the apex class
Select Id, Name, Report_ID__c, OwnerId, Attachment_Name__c, Email_Subject__c, Email_Body__c, Email_Recipients__c,Weekly_Days__c, Frequency__c, Monthly_Day__c, Last_Run__c from Report_Exporter__c

 
Raj VakatiRaj Vakati
Use this query
 
Select Id, Name, Report_ID__c,Owner.Name,Owner.Email, OwnerId, Attachment_Name__c, Email_Subject__c, Email_Body__c, Email_Recipients__c,Weekly_Days__c, Frequency__c, Monthly_Day__c, Last_Run__c from Report_Exporter__c

 
Satya Ranjan MohantySatya Ranjan Mohanty
Hi Raj,

Now the problem  is owner name is displaying but the email address showing who has Scheduled
the batch job.
we have tried with ord-wide-email address their the issue in every mail the display name will be same we need the owner name should displayed thr.
Any thoughts how to implement this reqiurement
Thank you very much
satya

 
Satya Ranjan MohantySatya Ranjan Mohanty
Hi Raj,
Any thoughts
satya