0

I need to be able to query for all attachments related to a single case, even the attachments related to emails on the case. I also need it this code to get all attachments if there are multiple emails related to the case.

    public static void transferAttachments(Case toCase, Parameter p) {
    set<Id> caseIdSet = new set<Id>();
    set<Id> toAttachmentIdSet = new set<Id>();
    set<Id> contentdocumentIdSet = new set<Id>();
    map<id, list<Id>> caseidToListofConDocIdsMap = new map<id, list<Id>>();
    map<Id, list<Id>> fromAttachmentsEmailMsgIdtoDocIdsMap = new map<id, list<Id>>();
    caseIdSet.add(toCase.id);
    caseIdSet.add(p.fromCase.id);
        set<Id> fromCaseContentDocIds = new set<Id>();
    map<Id, emailMessage> emailMessageMap = new map<Id, EmailMessage>();
    for(EmailMessage em : [SELECT Id, ContentDocumentIds, parentId from EmailMessage where ParentId =: p.fromCase.Id]) {
        emailMessageMap.put(em.parentId, em);
        for(id contentdocumentId : em.ContentDocumentIds) {
            fromCaseContentDocIds.add(contentdocumentId);
        }
    }
    system.debug(fromCaseContentDocIds);
    list<ContentDocumentLink> attachmentsToInsert = new list<ContentDocumentLink>();
    map<id,list<ContentDocumentLink>> caseIdToListOfAttachments = new map<id,list<ContentDocumentLink>>();
list<ContentDocumentLink> contentdocumentLinkList = [SELECT ContentDocumentId,Id,LinkedEntityId,ShareType FROM ContentDocumentLink WHERE LinkedEntityId =: toCase.id OR LinkedEntityId =: p.fromCase.id 
                                                    OR contentDocumentId IN: fromCaseContentDocIds];

I am attempting to query for email messages then make a map of those then query contentdocumentlinks since i should be able to retrieve the contentDocumentIds from emailmessage object. However my query is not pulling back the email attachments.

0

I created this method which returns a List<ContentVersion> of all [latest versions] of child attachments from a parent Id. It uses two SOQL queries.

public List<ContentVersion> getAllChildAttachments(Id parentId){
            list<Id> liChildDocumentIds = new List<Id>(); 
            for(ContentDocumentLink cdl :  [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId =: parentId]){
                liChildDocumentIds.add(cdl.ContentDocumentId);
            }

            return [SELECT Id, Title, ContentSize, Description, FileExtension, FileType, IsLatest, VersionData FROM ContentVersion WHERE ContentDocumentId IN: liChildDocumentIds AND IsLatest = true];

        } 

Hope that helps with your use case

  • Unfortunately this still did not return the attachments related to the child email messages. This only brought back the attachments linked directly to the case. Thank you for your answer though. – Logie C Apr 1 at 17:35
  • Perhaps you can modify my method. It seems you already have already have a list of contentDocumentIds, so try performing the query: List<Contentversion> liCV = [SELECT Id, ..... FROM ContentVersion WHERE ContentDocumentId IN: fromCaseContentDocIds]; – Z33dawg Apr 1 at 18:01
  • I actually found out what my issue was. If a case is created via email-to-case and there is an attachment with the email, that attachment is not saved as a file, it is saved as an Attachment. Therefore any query on contentversion/contentdocument stuff will never bring back these records and ill have to query separately. – Logie C Apr 1 at 18:20
  • I just noticed in your last query, maybe you need to change the last condition in your last SOQL : OR LinkedEntityId IN :fromCaseContentDocIds]; – Z33dawg Apr 1 at 18:21
  • glad you found your solution! Watch out for the day they decide to switch the email-to-case from the Attachment object to ContentDocs, (hopefully they inform us) – Z33dawg Apr 1 at 18:24

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.