[SalesForce] Allow user to insert File/ContentVersion when not Admin

I want to insert ContentVersion as a non-Admin Profile/User. When I run the code below I get the error below.
I tried to clone the System Administrator profile and call it nonAdmin, that did not work. I gave all the related CRUD permissions to the user: ContentVersion, Document and ContentDocument, same error.

Code

system.runAs(nonAdmin){
    insert new ContentVersion(
        title=title, 
        pathOnClient=pathOnClient, 
        versionData=EncodingUtil.base64Decode(body));   
}

Error

System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, You do not have permission to upload files.: []

EDIT:
Solution

It was a Trigger preventing the insertion.

Best Answer

FIELD_CUSTOM_VALIDATION_EXCEPTION implies there is either a custom validation rule on the object or trigger having some validation (using addError on the field) and this is not due to platform out of box error or a process stopping insertion of content object. To get it running look into the validation rules and create your test data so that it successfully bypasses the validation rule.

Related Topic