We have a situation where some users have migrated over to Lightning and turned on Email to case, with any attachment that comes in being converted over to Files.

This works well to show files under the Email History but more importantly under Email Files BUT for lightning users only. Now there is another set of existing users that are still in Classic using Cases. On Turning the the feature "Convert SF attachment to Files" the functionality in classic (custom solution) to move files from Email Message to the Cases file is now broken. The reason for this is there is some trigger that allows moving the Attachments from there over to Cases ( as attachment) which apparently does not work as it is expecting an attachment object where as it is receiving a file. Question is - Is there a way we can clone the behavior for moving attachment from Emails to Cases for Salesforce Files, so that is behaves similar to lighning.


  • Files are ContentVersion sobjects and support triggers. ContentDocumentLink is a junction between ContentVersion and Case.
    – cropredy
    Sep 15, 2019 at 0:11
  • Thanks @cropredy. I was able to write an after insert trigger on the ContentDocumentLink.
    – Raj
    Sep 18, 2019 at 0:24
  • Cool. Please write up your solution as an answer.
    – cropredy
    Sep 18, 2019 at 0:30

2 Answers 2

trigger trac_ContentDocumentLink on ContentDocumentLink ( after insert) {


public class ContentDocumentLinkHelper {

  public static void processAfterInsert(Map<Id, ContentDocumentLink> cdlMap) {

    Map<Id, Id> mapContentDocXEmailMessage = new Map<Id, Id>();

    for (ContentDocumentLink cdl : cdlMap.values()) {
            mapContentDocXEmailMessage.put(cdl.Id, cdl.LinkedEntityId);

    List<EmailMessage> emList = [
            SELECT id, ParentID, FromAddress, FromName, HasAttachment, Headers, Subject, ActivityID, ToAddress
            FROM EmailMessage
            WHERE id = :mapContentDocXEmailMessage.values()


    Map<Id, EmailMessage> mapCaseXEmailMessage = new Map<Id, EmailMessage>(emList);

    List<ContentDocumentLink> cdlupd = new List<ContentDocumentLink>();

    //Loop through all records in the Trigger.new collection
    for (ContentDocumentLink cdl : cdlMap.values()) {

        if (mapContentDocXEmailMessage.ContainsKey(cdl.Id)) {

            Id emailMessageId = mapContentDocXEmailMessage.get(cdl.Id);

            if (mapCaseXEmailMessage.ContainsKey(emailMessageId)) {

                EmailMessage obj = mapCaseXEmailMessage.get(emailMessageId);
                ContentDocumentLink c = new ContentDocumentLink();
                c.ContentDocumentId = cdl.ContentDocumentId;
                c.LinkedEntityId = obj.ParentID;
                c.ShareType = cdl.ShareType;
                c.Visibility = cdl.Visibility;

    insert cdlupd;

  • pls mark this as an answer
    – cropredy
    Sep 19, 2019 at 23:30

No we can't clone the behaviour. Files and attachments are stored in different objects and have their own way of implementing them.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .