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
Steven Cooper 3Steven Cooper 3 

Apex Trigger for Changing ContentDocument Owner Not Working

I have a custom object for an "Application" and each application has an evaluator (Lookup to user table). I want all files uploaded to be owned by the evaluator since other users might upload files but it's annoying that the actual evaluator can't delete them unless they have "Modify All Data" permissions. They also don't automatically attach to AdobeSign agreements because they are not owned by the senter. I wrote this trigger and it seems to work but when you try to upload the file, it gives you an error message in Salesforce, despite the log having no indication of an error. If  I change the trigger to "after insert" instead of "before insert" the file uploads successfully but the owner doesn't change. 
 
trigger CDL_OwnershipUpdate2021 on ContentDocumentLink (before insert) {

    for(ContentDocumentLink CDL: trigger.New)
    { 
        string parentId = CDL.LinkedEntityId;
        string newOwner = null;
        string docId = CDL.ContentDocumentId;
        
        List<Application__c> Apps = new List<Application__c>();
        
        Apps = [select Id, Evaluator__c from Application__c where Id = :parentId];
        
        if(Apps != null && Apps.size() > 0){
            
            System.debug('Checkpoint 1');
            
            for(Application__c app: Apps)
            {
                newOwner = app.Evaluator__c;
            }
        }
        
        ContentDocument doc = [select Id, OwnerId from ContentDocument where Id = :docId];
        
        if(newOwner != null){
            
            System.debug('Checkpoint 2');
            
            doc.OwnerId = newOwner;
            
            update doc;
        }   
    }
}

 
Abhishek BansalAbhishek Bansal
Hi Steven,

Can you let us know what error you are getitng?

Thanks,
Abhishek Bansal.
Steven Cooper 3Steven Cooper 3
Actually no, I can't because I can't find an error. In the developer consol the log says "Success" and no part of the log mentions an error. In the UI I get a red error that says the file couldn't be uploaded. I have an example of a log from trying to upload a file but unfortunately it's 12,000 lines long and over 1 MB so I don't think it will let me post it here. I can't even get it to work on pastepin.com or justpasteit.com because it's so large. 

I get an error in the UI but none of the logs for that transaction show anything obviously weird. 
Abhishek BansalAbhishek Bansal
Hi Steven,

Is it possible to connect on a call and see what exactly the issue is? You can contact me on:
Gmail: abhibansal2790@gmail.com
Skype: abhishek.bansal2790

Thanks,
Abhishek Bansal.
AbhishekAbhishek (Salesforce Developers) 
https://salesforce.stackexchange.com/questions/273681/trigger-is-not-updating-the-first-task-record-on-contact