Salesforce Auto Relate Email Message Attachment

The Salesforce Lightning plugin for Outlook allows logging emails directly into Salesforce. However, workflow or process builder cannot trigger events on these logged emails (vote for that feature here [Salesforce Ideas]).

By default all logged email file attachments are related to the EmailMessage object that is created by the plugin. The EmailMessage can be related to any object via the plugin; however, the plugin does not allow the file attachment to also be related to an object. In fact, the share button on a file detail page only allows sharing to a group or person – not an object. Here’s the trigger to circumvent that restriction.