I want to copy files (not attachment sObject) from old case to the new one as feeditem. What sObjects should I copy?
1 Answer
Why clone files and increase file Storage when you can link same file to multiple cases.
The junction Object from Case and File is called as ContentDocumentLink.
So what we can do is create another link of that file for new case.
public static void cloneFilesFromOldCaseToNew(String oldCaseId ,String newCaseId ){
List<ContentDocumentLink> toBeClonedContentDocumentLinks = [SELECT ContentDocumentId,Id,IsDeleted,LinkedEntityId,ShareType,SystemModstamp,Visibility FROM ContentDocumentLink where LinkedEntityId=:oldCaseId];
for(ContentDocumentLink cdl : toBeClonedContentDocumentLinks){
cdl.Id=null;
cdl.LinkedEntityId = newCaseId;
}
insert toBeClonedContentDocumentLinks;
}
-
good point! Is it ok to do cloning with the same record but empty id field? Nov 29, 2018 at 14:50
-
when you do a clone and create a new record, its has a new ID. I made the Id as null so that I can use insert command. Insert command only works if there is no ID. After inset a new record will be created and new ID will be associated Nov 29, 2018 at 14:52
-
-
It is for SF files only, You have to investigate for FeedItem attachment as its a different table so different query and approach Nov 30, 2018 at 10:55
-
that was the reason of the question :) i don't know exactly what the table I should to query. Nov 30, 2018 at 10:59