function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
divya1234divya1234 

SOQL query to retrieve files for cases

I want to find all case parent record id which contains the files 
Best Answer chosen by divya1234
PreyankaPreyanka
Hello Rajini,

If you want to find the files attached with case you can use below query.

SELECT ContentDocumentId, LinkedEntityId  FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM Case ) and LinkedEntity.Type='Case'

In the above query ContentDocumentId is the Id of the files and LinkedEntityId is the id of case.

Thanks
Preyanka

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Rajni,

Following links should be helpful to understand how to get a query for the files from the document object: (replace job applications with document object)

Also, refer: Hope this helps.

Mark this as solved if it's resolved.

Thanks,
Nagendra
Shiva RajendranShiva Rajendran
Hi Rajini,
I have written a simple trigger which does the same functionality as you expected.
 
Trigger CaseAttachmentTrigger on case(After insert)
{
        //retrieve all the cases with the parentId
      List<Case> allCases=[select id,parentId from case where id in :Trigger.new];
        
         Set<Id> allParentCase=new Set<Id>();
       for(Case c:allCases)
       {
          allParentCase.add(c.parentId);
       }
  // allParentCase contain all unique parent cases id

   List<Attachment> allAttachmentParentContains=[select id,Body,BodyLength from attachment where parentId in :allParentCase];

//All the attachment of parentCase can be obtained here


//  List<Attachment> allAttachmentParentContains=[select id from attachment where parentId in :allParentCase];
//better use this to not hit view state error

           
}
//note that in here we have retrieved the body of the attachment ,hence the controller can throw view state error if there are many attachments in the org
//Better only take the id of the attachment and retrieve its body only necessary in code.


Let me know if you need further help .

Thanks and Regards,
Shiva RV
 
PreyankaPreyanka
Hello Rajini,

If you want to find the files attached with case you can use below query.

SELECT ContentDocumentId, LinkedEntityId  FROM ContentDocumentLink where LinkedEntityId in ( SELECT Id FROM Case ) and LinkedEntity.Type='Case'

In the above query ContentDocumentId is the Id of the files and LinkedEntityId is the id of case.

Thanks
Preyanka
This was selected as the best answer
PreyankaPreyanka
Hello Rajini,

As per my understanding ParentId of attachment is ID of the parent object of the attachment (like the Id of case, Account etc. In this scenario its case as we specify that in query) and LinkedEntityId also refers to the ID of the objects in this scenario, its case as we specify that in query, hence we get the Id of case in both above query.

Here what you want refer as case parent id : Id of a case object or Parent case Id of a case?

If you want to refer the Id of a case then you may get that from above query. 

Thanks
divya1234divya1234
Yes Preyanka you correct here LinkedEntityId  will be case parent id. thankyou so much for you help :)