• Rd
  • NEWBIE
  • 10 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
I have created a lookup field on the content version object that I am trying to auto populate whenever a file is uploaded on that object.

I created a trigger but it only works sometimes, and other times it doesn't.

This is the code that I have:

trigger UpdateCVLookup on ContentVersion (after insert) {
    
    Set<Id> contentDocumentIdSet = new Set<Id>();
    
    for(ContentVersion cv:trigger.new)
    {
        if(cv.ContentDocumentId != null)
        {
            contentDocumentIdSet.add(cv.ContentDocumentId);
        }
    }
    
    ContentDocumentLink cdl = [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN:contentDocumentIdSet LIMIT 1];
 
    // Project__c can be replaced with the object that has the lookup relationship.

    List<Project__c> upList = [SELECT Id, name FROM Project__c where Id =:cdl.LinkedEntityId];   
    
    //In the following line, make sure to replace Project__c(The lookup field name) with the api name of the lookup field on the content version object.
    List<ContentVersion> cvList = [SELECT Id, Project__c, ContentDocumentId FROM ContentVersion where Id IN: Trigger.newMap.keySet()];
    
    for(ContentVersion cv:cvList)
    {
        if(upList.size() > 0)
        {//In the next line Project__c is referring to the lookup object.
            for(Project__c p : upList){
                //The following line Project__c is referring to the lookup field on Content Versions.
                cv.Project__c = p.Id; 
            }
        }
        else if(upList.size() > 0){
            //The next two lines containing Project__c are referring to the lookup field on Content Versions
            cv.Project__c = cdl.LinkedEntityId;
        } else {
            //This line below is required so that files can still be uploaded to other objects.
            cv.Project__c = null;
        }
    }
    update cvList;
}
  • September 14, 2020
  • Like
  • 0
I have created a lookup field on the content version object that I am trying to auto populate whenever a file is uploaded on that object.

I created a trigger but it only works sometimes, and other times it doesn't.

This is the code that I have:

trigger UpdateCVLookup on ContentVersion (after insert) {
    
    Set<Id> contentDocumentIdSet = new Set<Id>();
    
    for(ContentVersion cv:trigger.new)
    {
        if(cv.ContentDocumentId != null)
        {
            contentDocumentIdSet.add(cv.ContentDocumentId);
        }
    }
    
    ContentDocumentLink cdl = [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN:contentDocumentIdSet LIMIT 1];
 
    // Project__c can be replaced with the object that has the lookup relationship.

    List<Project__c> upList = [SELECT Id, name FROM Project__c where Id =:cdl.LinkedEntityId];   
    
    //In the following line, make sure to replace Project__c(The lookup field name) with the api name of the lookup field on the content version object.
    List<ContentVersion> cvList = [SELECT Id, Project__c, ContentDocumentId FROM ContentVersion where Id IN: Trigger.newMap.keySet()];
    
    for(ContentVersion cv:cvList)
    {
        if(upList.size() > 0)
        {//In the next line Project__c is referring to the lookup object.
            for(Project__c p : upList){
                //The following line Project__c is referring to the lookup field on Content Versions.
                cv.Project__c = p.Id; 
            }
        }
        else if(upList.size() > 0){
            //The next two lines containing Project__c are referring to the lookup field on Content Versions
            cv.Project__c = cdl.LinkedEntityId;
        } else {
            //This line below is required so that files can still be uploaded to other objects.
            cv.Project__c = null;
        }
    }
    update cvList;
}
  • September 14, 2020
  • Like
  • 0