Content Version Trigger error 'bad value for restricted picklist' - Answers - Salesforce Trailblazer Community
Trailblazer Community
Ask Search:
Inni ShiInni Shi 

Content Version Trigger error 'bad value for restricted picklist'

Hi, I'm trying to write a simple trigger to change the picklist value on 'Content Version' object when a new attachment is uploaded but I'm getting error 'bad value for restricted picklist' I check it is the correct value. not sure where I'm going wrong. Any help would be really appreciated.

trigger test on ContentVersion (After insert) {
for(ContentVersion opp: trigger.new){
   opp.Title = 'Private on Records';
    opp.SharingPrivacy = 'Freeze Sharing On';
}}
Best Answer chosen by Inni Shi
Inni ShiInni Shi
It turned out that I should be using API values and not the display value.
label: Visible to Anyone With Record Access
API value: N
label: Private on Records
API value: P
https://salesforce.stackexchange.com/questions/291019/content-version-error-bad-value-for-restricted-picklist 

All Answers

Piyush SinghalPiyush Singhal
Hi, inni shi

Controls sharing privacy for a file. Only administrators and file owners with Collaborator access to the file can modify this field. Default is Visible to Anyone With Record Access. When set to Private on Records, the file is private on records but can be shared selectively with others.

If you want to change this field value than you have to logged in as a administrator. And change your trigger to before insert

trigger test on ContentVersion (Before insert) {
for(ContentVersion opp: trigger.new){
   opp.Title = 'Private on Records';
    opp.SharingPrivacy = 'Freeze Sharing On';
}} 
Amnon KruviAmnon Kruvi
I think you confused the values between Title and SharingPrivacy. The SharingPrivacy field can accept "Visible to Anyone With Record Access" or "Private on Records", whereas the title can be free text. Try switching the two fields around.

Also, I agree with Piyush that this should be a before trigger rather than after. If you set it as After, it requires a second DML to update the record.
Inni ShiInni Shi
Hi Piyush, 
   I tried with 'Before Insert' but the same error.
@ Ammon you are right, sorry about the confusion. so currently my trigger looks like this.

trigger test on ContentVersion (Before Insert) {
for(ContentVersion opp: trigger.new){
opp.SharingPrivacy  = 'Private on Records';}}

Just so you know the business requirement here is  "we are looking to add quite a lot of attachments to custom object records but we only want certain people to have view access on those attachments/files and not everyone who has got access to those records in question, Ideally, we want a group to be added and only people in that group to have view access but I'm not a programmer so I thought to make those attachments 'private on Records' by default so that only people above in the role hierarchy will have the visibility"
based on this article https://help.salesforce.com/articleView?id=collab_files_make_private_on_record.htm&type=5) 
Piyush SinghalPiyush Singhal

Hi, inni shi 

As per your requirement, you can create a group of the user whom you want to give access and for that, you have to log in as an Administrator.
Only administrators and delegated administrators can create and edit public groups, but anyone can create and edit their own personal groups.
I mentioned a link below which shows steps for creating the group as per your requirement.

https://developer.salesforce.com/docs/atlas.enus.securityImplGuide.meta/securityImplGuide/creating_and_editing_groups.htm

Thank You
Piyush Singhal

Inni ShiInni Shi
It turned out that I should be using API values and not the display value.
label: Visible to Anyone With Record Access
API value: N
label: Private on Records
API value: P
https://salesforce.stackexchange.com/questions/291019/content-version-error-bad-value-for-restricted-picklist 
This was selected as the best answer