In our Org, Email-to-Case Attachments are also stored as Files. And that the default owner is an Admin. When a case with an attachment is reassigned to another user (non-admin), the attachment ownership is not reassigned. This causes a problem when the new Case owner tries to edit the details of the attachment/file. I got this Apex example but when I tried it, it didn't work. Appreciate any help and guidance on this problem.
trigger ReassignContentVersionOwner on ContentVersion (after insert) {
List<ContentVersion> versionsToUpdate = new List<ContentVersion>();
Set<Id> contentDocumentIds = new Set<Id>();
for (ContentVersion cv : Trigger.new) {
contentDocumentIds.add(cv.ContentDocumentId);
}
Map<Id, ContentDocumentLink> docLinkMap = new Map<Id, ContentDocumentLink>([
SELECT Id, ContentDocumentId, LinkedEntityId
FROM ContentDocumentLink
WHERE ContentDocumentId IN :contentDocumentIds
AND LinkedEntityId.Type = 'Case'
]);
Set<Id> caseIds = new Set<Id>();
for (ContentDocumentLink cdl : docLinkMap.values()) {
caseIds.add(cdl.LinkedEntityId);
}
Map<Id, Case> caseMap = new Map<Id, Case>([SELECT Id, OwnerId FROM Case WHERE Id IN :caseIds]);
for (ContentVersion cv : Trigger.new) {
ContentDocumentLink cdl = docLinkMap.get(cv.ContentDocumentId);
if (cdl != null && caseMap.containsKey(cdl.LinkedEntityId)) {
versionsToUpdate.add(new ContentVersion(
Id = cv.Id,
OwnerId = caseMap.get(cdl.LinkedEntityId).OwnerId
));
}
}
if (!versionsToUpdate.isEmpty()) {
update versionsToUpdate;
}
}
ContentDocument
). Use edit and be more specific about what fields are changed by the Case Owner.