• Steven Cooper 3
  • NEWBIE
  • 0 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
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;
        }   
    }
}

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