0

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;
}

}

1
  • 1
    Your trigger assume a new version of the file is uploaded but the question implies the new Case owner is editing the details like the name of the file (these fields are in the ContentDocument). Use edit and be more specific about what fields are changed by the Case Owner.
    – cropredy
    Commented Jun 21 at 21:36

1 Answer 1

0

Looks like you are on the right track. One things that you need to check: Ensure that OwnerId can be updated on the ContentVersion. I did some modifications on your code. Please try this:

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);
}

// Fetching ContentDocumentLinks linked to Cases
Map<Id, ContentDocumentLink> docLinkMap = new Map<Id, ContentDocumentLink>([
    SELECT Id, ContentDocumentId, LinkedEntityId 
    FROM ContentDocumentLink 
    WHERE ContentDocumentId IN :contentDocumentIds 
    AND LinkedEntityId IN (SELECT Id FROM Case)
]);

Set<Id> caseIds = new Set<Id>();
for (ContentDocumentLink cdl : docLinkMap.values()) {
    caseIds.add(cdl.LinkedEntityId);
}

// Fetching Cases with their Owners
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)) {
        //Changing the owner of the ContentVersion
        ContentVersion updatedVersion = new ContentVersion(
            Id = cv.Id,
            OwnerId = caseMap.get(cdl.LinkedEntityId).OwnerId
        );
        versionsToUpdate.add(updatedVersion);
    }
}

if (!versionsToUpdate.isEmpty()) {
    update versionsToUpdate;
}

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .