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.
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.
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
Following links should be helpful to understand how to get a query for the files from the document object:
- http://developer.force.com/cookbook/recipe/retrieving-email-attachments-and-associating-them-with-records
(replace job applications with document object)Also, refer:
- http://salesforce.stackexchange.com/questions/11841/is-there-a-superclass-for-document-and-attachment
- http://salesforceapexcodecorner.blogspot.sg/2011/09/attach-document-in-attachment-object.html
Hope this helps.Mark this as solved if it's resolved.
Thanks,
Nagendra
I have written a simple trigger which does the same functionality as you expected.
//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
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
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