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.

trigger AWS_AssignEmailAttachmentRelated on ContentDocumentLink (after insert) {
        
    for(ContentDocumentLink link : Trigger.new) {
        
        // check if this is an email message
        Id linkedEmailId = link.LinkedEntityId;
                
        if(linkedEmailId.getSobjectType().getDescribe().getName().equals('EmailMessage')) {
            
            // get email's related to
            EmailMessage message = [SELECT Id, RelatedToId FROM EmailMessage WHERE Id = :linkedEmailId];
                        
            if(message.RelatedToId != null) {
                
                // insert link to related object
                ContentDocumentLink myLink = new ContentDocumentLink(
                    ContentDocumentId = link.ContentDocumentId,
                    LinkedEntityId = message.RelatedToId,
                    ShareType = 'V',
                    Visibility = 'AllUsers'
                 );
                 insert myLink;
            }
        }
        
    }

The trigger checks a ContentDocumentLink object (an object linking files to objects similar to custom junction objects) for an EmailMessage entry. When it finds an EmailMessage linked to a file, it inserts a new file link to the email’s related object.

Here’s the test class for the above trigger.

@isTest
public class AWS_AssignEmailAttachmentRelatedTest {

    static testmethod void AutoRelateEmailMessageTriggerTest() {
        
        Test.startTest();

        // Create Account
        Id accountRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Business').getRecordTypeId();
        Account aws_account = new Account(
            RecordTypeId=accountRecordTypeId, 
            Name='AWS Account', 
            CanaryAMS__Active__c='Yes', 
            CanaryAMS__Business_Structure__c='LLC', 
            Industry='Insurance',
            Sic='123',
            CanaryAMS__FEIN__c='ABC'
        );
        insert aws_account;
        
        // Create a ContentVersion
        ContentVersion aws_content_ver = new ContentVersion();
        aws_content_ver.Title = 'AWS Doc';
        aws_content_ver.ContentUrl= 'test.com';
        insert aws_content_ver;
                
        // Insert email
        EmailMessage mEmail = new EmailMessage(
            Subject = 'Test Logged Email',
            RelatedToId = aws_account.Id
        );
        insert mEmail;
        
        // Create Content Document Link
        ContentDocumentLink aws_document_link = new ContentDocumentLink(
            ContentDocumentId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =: aws_content_ver.Id].ContentDocumentId,
            LinkedEntityId = mEmail.Id,
            ShareType = 'V',
            Visibility = 'AllUsers'
        );
        insert aws_document_link;
        
        Test.stopTest();
    }
    
}