1

In the finish method for a batch job it is common to send an email with code like:

global void finish(Database.BatchableContext BC){
   // Get the ID of the AsyncApexJob representing this batch job
   // from Database.BatchableContext.
   // Query the AsyncApexJob object to retrieve the current job's information.
   AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
      TotalJobItems, CreatedBy.Email
      FROM AsyncApexJob WHERE Id =
      :BC.getJobId()];
   // Send an email to the Apex job's submitter notifying of job completion.
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {a.CreatedBy.Email};
   mail.setToAddresses(toAddresses);
   mail.setSubject('Apex Sharing Recalculation ' + a.Status);
   mail.setPlainTextBody
   ('The batch Apex job processed ' + a.TotalJobItems +
   ' batches with '+ a.NumberOfErrors + ' failures.');
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}

Source - Using Database.BatchableContext.

When the email from Messaging.sendEmail arrives, how can the Org that sent it be identified?

Note that the question isn't specifically about batch jobs, they are just a common example of where the email would be sent (perhaps on a regular schedule) and the originating Org might be difficult to identify. In the above example it would be easy enough to append UserInfo.getOrganizationId() to the message body.

1

Reid Carlberg found a solution where the originating Org Id appears in the SMTP email header X-SFDC-LK. There is also X-SFDC-User with the senders User ID.

X-SFDX-X SMTP Header

See also:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.