2

enter image description here For Email Services, we know that incoming mails to this email id is processed in the mentioned class, for my case it is InboundEmailService. But since there is an option of adding multiple email addresses for the same email service, how can we differentiate inside the class in which email address is my email received. In general, if there are multiple email addresses in a email service, how can we access and customize the process inside the class?

1

@Cropredy gave an excellent approach of defining a different context user for each inbound email service. But if you are stuck with user license limitation I would suggest you to get along with a different approach.

Your EmailService class has handleInboundEmail method which has Messaging.InboundEmail as a parameter. This InboundEmail class has methods like toAddresses and ccAddresses which provides us on which email addresses the email was targeted. You can parse those email addresses and divert your logic.

References: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_inbound_inbound.htm

4

One strategy is to use a different context user for each inbound email services address. Then, in the inbound email services handler, you can use UserInfo class to route logic accordingly

if (UserInfo.getUserName() == 'foo') {
   // logic path 1
}
else
if (UserInfo.getUserName() == 'bar') {
   // logic path 2
}

having unique context users for each inbound email path is helpful for traceability as to why a database record was updated at a certain time (as it will appear in the last modified by and depending on the field, in the field history related list

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.