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?
2 Answers
@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.
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