You need to sign in to do that
Don't have an account?
Sylvie 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.
Thanks in advance for your help.
Sylvie
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
All Answers
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?https://success.salesforce.com/issues_view?id=a1p3A0000001CdCQAU
https://help.salesforce.com/articleView?id=000264824&language=en_US&type=1
Regards,
Waqar
It is not a permission issue because when I remove the trigger I can updload any files without any error message.
Thanks.
Sylvie
I have checked all your points and none of them apply to my situation.
Please find below script exception.
I have tried to add limit 1 to the following SOQL And got another error on line 24. My development skills ended here!
Thank you for your help.
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.
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.
Add the following debug statements to inspect the values within the c and accList variables and verify the issue.
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.
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?