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
Sylvie SerpletSylvie Serplet 

Trigger on ContentVersion

Hi All,

I have written a Trigger to update a custom field (Account__c) on ContentVersion.
The Trigger does not have any error but when I try to updload a file in an Account I got an error message "Can't updload (file name)".
Something wrong with the Trigger but I can't figure it why. 
trigger UpdateAccountonFile on ContentVersion (after insert) {

    Set<Id> docIds = new Set<Id>();
    
    for(ContentVersion cv : trigger.new){  
       
      if(cv.ContentDocumentId != null)
            {
                docIds.add(cv.ContentDocumentId);
        }  
    }     
  ContentDocumentLink c = [SELECT ContentDocument.Id, ContentDocument.Title, Id, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocument.Id In:docIds ];   
    
   List<Account> accList = [SELECT Id, name FROM Account WHERE Id =:c.LinkedEntityId ];            
    
    for(ContentVersion cv : trigger.new){      
        
        if(accList.size() > 0){
            for(Account a : accList){
                cv.Account__c = a.Id;
            }
        }
        else{
            cv.Account__c = null; 
        }            
    }
}

Thanks in advance for your help.
Sylvie 
 
Best Answer chosen by Sylvie Serplet
Sylvie SerpletSylvie Serplet
Code that works and update a custom field on ContentVersion with a LinkedEntityId on ContentDocumentLink.
 
trigger UpdateAccountonFile 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];
    
    List<Account> accList = [SELECT Id, name FROM Account where Id =:cdl.LinkedEntityId];   
    
    List<ContentVersion> cvList = [SELECT Id, Account__c, ContentDocumentId FROM ContentVersion where Id IN: Trigger.newMap.keySet()];
    
    for(ContentVersion cv:cvList)
    {
        if(accList.size() > 0)
        {
            for(Account a : accList){
                cv.Account__c = a.Id; 
            }
        }
        else{
            cv.Account__c = null;
        }           
    }
    update cvList;
}

 

All Answers

jigarshahjigarshah
Sylvie,

Lookign at your Apex trigger, I don't see any specific constraint set on naming conventions of the file within the code shared above.
  • Check if a file with the same name already exists. Duplicate file names could possibly be an issue.
  • Do you have specific validations on the naming format of the file that is being uploaded since the error you mentioned indicates that. Also, you can try providing a simple name without any special characters to verify this.
  • Also, are you restricting the files to be uploaded to a specific file extension? If yes, check the extension of the file being uploaded.
  • Check the file size as well since Salesforce has limits on the size of the files that can be uploaded.
Also, have you tried looking at Setup > Debug Logs to understand the exact error message which is causing the failure? Can you share that message here?
Waqar Hussain SFWaqar Hussain SF
It seems some permission issue. Please see below articles which explain the same issue.

https://success.salesforce.com/issues_view?id=a1p3A0000001CdCQAU
https://help.salesforce.com/articleView?id=000264824&language=en_US&type=1

Regards,
Waqar
Sylvie SerpletSylvie Serplet
Hi Waqar,
It is not a permission issue because when I remove the trigger I can updload any files without any error message.
Thanks.
Sylvie
Sylvie SerpletSylvie Serplet
Hi Jigarshah,

I have checked all your points and none of them apply to my situation.

Please find below script exception.
Apex script unhandled trigger exception by user/organization: 00528000002LXct/00D0k0000000b0N Source organization: 00D28000000cOTX (null)
UpdateAccountonFile: execution of AfterInsert

caused by: System.QueryException: List has more than 1 row for assignment to SObject

Trigger.UpdateAccountonFile: line 12, column 1

Debug Log:
42.0 APEX_CODE,FINEST;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WAVE,INFO;WORKFLOW,INFO
07:57:42.0 (326275)|USER_INFO|[EXTERNAL]|00528000002LXct|sylvie.serplet2@ausure.com.au.partials|Australian Eastern Standard Time|GMT+10:00
07:57:42.0 (361237)|EXECUTION_STARTED
07:57:42.0 (366193)|CODE_UNIT_STARTED|[EXTERNAL]|01q0k0000008guU|UpdateAccountonFile on ContentVersion trigger event AfterInsert
07:57:42.0 (402543)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
07:57:42.0 (826104)|HEAP_ALLOCATE|[72]|Bytes:3
07:57:42.0 (858554)|HEAP_ALLOCATE|[77]|Bytes:152
07:57:42.0 (871517)|HEAP_ALLOCATE|[342]|Bytes:408
07:57:42.0 (883128)|HEAP_ALLOCATE|[355]|Bytes:408
07:57:42.0 (894892)|HEAP_ALLOCATE|[467]|Bytes:48
07:57:42.0 (918551)|HEAP_ALLOCATE|[139]|Bytes:6
07:57:42.0 (1022074)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
07:57:42.0 (1032945)|VARIABLE_SCOPE_BEGIN|[1]|this|UpdateAccountonFile|true|false
07:57:42.0 (1099248)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x21348597
07:57:42.0 (1151178)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:16
07:57:42.0 (1158773)|VARIABLE_SCOPE_BEGIN|[1]|this|UpdateAccountonFile|true|false
07:57:42.0 (1174190)|VARIABLE_ASSIGNMENT|[1]|this|{}|0x21348597
07:57:42.0 (1181581)|STATEMENT_EXECUTE|[1]
07:57:42.0 (1184323)|STATEMENT_EXECUTE|[3]
07:57:42.0 (1200446)|HEAP_ALLOCATE|[3]|Bytes:4
07:57:42.0 (1269577)|HEAP_ALLOCATE|[50]|Bytes:5
07:57:42.0 (1291952)|HEAP_ALLOCATE|[56]|Bytes:5
07:57:42.0 (1301479)|HEAP_ALLOCATE|[64]|Bytes:7
07:57:42.0 (1346271)|VARIABLE_ASSIGNMENT|[3]|this.docIds|[]|0x21348597
07:57:42.0 (1557555)|HEAP_ALLOCATE|[5]|Bytes:5
07:57:42.0 (1580935)|VARIABLE_SCOPE_BEGIN|[5]|cv|ContentVersion|true|false
07:57:42.0 (2137968)|VARIABLE_ASSIGNMENT|[5]|cv|{"Origin":"C","LastModifiedDate":"2018-04-03T21:57:42.000Z","SharingPrivacy":"N","ContentBodyId":"05T0k000000Pm4fEAC","ContentSize":14811,"PathOnClient":"Book1.xlsx","PositiveRatingCount":0,"FirstPublishLocation (2 more) ...":"0010k00000S3WmYAAV","ContentModifiedById":"00528000002LXctAAG","CreatedById":"00528000002LXctAAG","OwnerId":"00528000002LXctAAG","RatingCount":0,"RecordTypeId":"0120k0000004vIOAAY","PublishStatus":"P","NegativeRatingCount":0,"IsDeleted":false,"IsAssetEnabled":false,"Title":"Book1","Owner_Name__c":"Sylvie Serplet","ContentDocumentId":"0690k0000005fXqAAI","IsLatest":true,"VersionNumber":"1","SystemModstamp":"2018-04-03T21:57:42.000Z","IsMajorVersion":false,"FileType":"EXCEL_X","ContentModifiedDate":"2018-04-03T21:57:42.000Z","ContentLocation":"S","SharingOption":"A","CreatedDate":"2018-04-03T21:57:42.000Z","Id":"0680k0000005gc3AAA","LastModifiedById":"00528000002LXctAAG"}|0x6bb0bf91
07:57:42.0 (2160285)|STATEMENT_EXECUTE|[5]
07:57:42.0 (2179053)|STATEMENT_EXECUTE|[8]
07:57:42.0 (2180678)|STATEMENT_EXECUTE|[9]
07:57:42.0 (2251674)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:4
07:57:42.0 (2272855)|HEAP_ALLOCATE|[5]|Bytes:5
07:57:42.0 (2288131)|VARIABLE_ASSIGNMENT|[5]|cv|null|
07:57:42.0 (2293266)|STATEMENT_EXECUTE|[12]
07:57:42.0 (2296786)|HEAP_ALLOCATE|[12]|Bytes:129
07:57:42.0 (2313227)|HEAP_ALLOCATE|[12]|Bytes:4
07:57:42.0 (2323830)|HEAP_ALLOCATE|[12]|Bytes:7
07:57:42.0 (2358854)|HEAP_ALLOCATE|[12]|Bytes:8
07:57:42.0 (2370818)|HEAP_ALLOCATE|[12]|Bytes:8
07:57:42.0 (20314362)|SOQL_EXECUTE_BEGIN|[12]|Aggregations:0|SELECT ContentDocument.Id, ContentDocument.Title, Id, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocument.Id = :tmpVar1
07:57:42.0 (29757885)|SOQL_EXECUTE_END|[12]|Rows:2
07:57:42.0 (29777397)|HEAP_ALLOCATE|[12]|Bytes:12
07:57:42.0 (29797640)|HEAP_ALLOCATE|[12]|Bytes:288
07:57:42.0 (29842188)|HEAP_ALLOCATE|[12]|Bytes:12
07:57:42.0 (29854963)|HEAP_ALLOCATE|[12]|Bytes:45
07:57:42.0 (29921259)|HEAP_ALLOCATE|[12]|Bytes:54
07:57:42.0 (30131845)|FATAL_ERROR|System.QueryException: List has more than 1 row for assignment to SObject

Trigger.UpdateAccountonFile: line 12, column 1
07:57:42.0 (30143613)|FATAL_ERROR|System.QueryException: List has more than 1 row for assignment to SObject

Trigger.UpdateAccountonFile: line 12, column 1
07:57:42.30 (30187192)|CUMULATIVE_LIMIT_USAGE
07:57:42.30 (30187192)|LIMIT_USAGE_FOR_NS|(default)|
  Number of SOQL queries: 1 out of 100
  Number of query rows: 2 out of 50000
  Number of SOSL queries: 0 out of 20
  Number of DML statements: 0 out of 150
  Number of DML rows: 0 out of 10000
  Maximum CPU time: 0 out of 10000
  Maximum heap size: 0 out of 6000000
  Number of callouts: 0 out of 100
  Number of Email Invocations: 0 out of 10
  Number of future calls: 0 out of 50
  Number of queueable jobs added to the queue: 0 out of 50
  Number of Mobile Apex push calls: 0 out of 10

07:57:42.30 (30187192)|CUMULATIVE_LIMIT_USAGE_END

07:57:42.0 (30969805)|CODE_UNIT_FINISHED|UpdateAccountonFile on ContentVersion trigger event AfterInsert
07:57:42.0 (31694081)|EXECUTION_FINISHED

I have tried to add limit 1 to the following SOQL
ContentDocumentLink c = [SELECT ContentDocument.Id, ContentDocument.Title, Id, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocument.Id In:docIds Limit 1];
And got another error on line 24.
cv.Account__c = null;
My development skills ended here!

Thank you for your help.
Sylvie

 
jigarshahjigarshah
Sylvie,

The following error "System.QueryException: List has more than 1 row for assignment to SObject" occurs because the filter clause i.e. the Where clause in your query is returning more than 1 record of type ContentDocumentLink. Basically it returns a List<ContentDocumentLink>.

However, your code at line # 12 assumes that the query will return a single record and hence the issue.
ContentDocumentLink c = [SELECT ContentDocument.Id, ContentDocument.Title, Id, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocument.Id In :docIds];

Inspite of you adding a Limit clause, the right record is not being picked up and this leads to the following Soql query at line # 14 is returning 0 Account records. This leads the else clause being executed which sets the Account__c field to null.
List<Account> accList = [SELECT Id, name FROM Account WHERE Id = :c.LinkedEntityId];

Add the following debug statements to inspect the values within the c and accList variables and verify the issue.
ContentDocumentLink c = [SELECT ContentDocument.Id, ContentDocument.Title, Id, LinkedEntityId 
						     FROM ContentDocumentLink 
						     WHERE ContentDocument.Id In :docIds ];   
    
	System.debug('****c -> ' + c);
   
    List<Account> accList = [SELECT Id, name FROM Account WHERE Id = :c.LinkedEntityId ];

    System.debug('****accList -> ' + accList);

The issue is a logical issue with your Soql query not retrieving the ContentDocumentLink record. The solution for this is to include appropriate condition in your Where clause instead of the Limit clause.
Sylvie SerpletSylvie Serplet
Code that works and update a custom field on ContentVersion with a LinkedEntityId on ContentDocumentLink.
 
trigger UpdateAccountonFile 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];
    
    List<Account> accList = [SELECT Id, name FROM Account where Id =:cdl.LinkedEntityId];   
    
    List<ContentVersion> cvList = [SELECT Id, Account__c, ContentDocumentId FROM ContentVersion where Id IN: Trigger.newMap.keySet()];
    
    for(ContentVersion cv:cvList)
    {
        if(accList.size() > 0)
        {
            for(Account a : accList){
                cv.Account__c = a.Id; 
            }
        }
        else{
            cv.Account__c = null;
        }           
    }
    update cvList;
}

 
This was selected as the best answer
Sanchivan SivadasanSanchivan Sivadasan
Hey Guys,

I am trying to write a trigger to auto create ContentDistribution records. So I started creating a trigger on ContentVersion and I am getting an error "Variable does not exist: ContentDocumentId". I am not sure why I am getting this error. Does any of you know why? 
 
trigger ContentVersionTrigger on ContentVersion (after insert) {
	Set<Id> contentDocumentIds = new Set<Id>();
    for(ContentVersion contentVersion : Trigger.New) {
        contentDocumentIds.add(contentVersion.ContentDocumentId);
    }
}

 
Himanshu Shekhar 76Himanshu Shekhar 76
@Sylvie Serple Any luck on this issue?