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
Leah SchneiderLeah Schneider 

Sharing files with Apex

I would like to set up a process that all files are editable by the whole company. I can set each file manually to have whole company collaboration but this is a pain and forces users to memorize steps. I tried to have a trigger on insert that would add a ContentDocumentLink with a link to the org Id and sharing set to C. As far as I can tell, the documentation out there says that what I'm doing is correct.

Apex is below.  It hits the try/ catch below and fails with "12:24:06:252 USER_DEBUG [16]|DEBUG|Exception occurred on insert: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_OR_READONLY, Invalid sharing type C: [ShareType]"
Best Answer chosen by Leah Schneider
Leah SchneiderLeah Schneider
Figured it out.  It seems in no situations should you have a trigger on ContentDocument.  It needs to be on ContentVersion.  Something to do with order of operations even though it's less efficient.

All Answers

Raj VakatiRaj Vakati
Share the code  to the issue .. please refer this code 

 
ContentDocumentLink cdl = new ContentDocumentLink();
cdl.ContentDocumentId = [SELECT Id, ContentDocumentId FROM ContentVersion WHERE Id =: cv.Id].ContentDocumentId;
cdl.LinkedEntityId = '00590000000a6dP';
cdl.ShareType = 'V';
insert cdl;

 
Leah SchneiderLeah Schneider
I'm not sure if I can edit.  Sorry about that.  I did this in a rush:
 
trigger setContentDocumentCollaboration on ContentDocument (after insert) {
    for (ContentDocument cd : Trigger.New) {
        String orgId = UserInfo.getOrganizationId();
        system.debug(orgId);
        ContentDocumentLink[] cdl = [SELECT LinkedEntityId, ContentDocumentId FROM ContentDocumentLink WHERE ContentDocumentId =: cd.Id AND LinkedEntityId =: orgId];
        if (cdl.size() == 0) {
            system.debug(cd.Id);
            ContentDocumentLink cdli = new ContentDocumentLink(ContentDocumentId = cd.Id, LinkedEntityId = orgId, ShareType = 'C', Visibility = 'AllUsers');
            //upsert cdli;
            try {
                
                insert(cdli);
                
            } catch(DmlException e) { System.debug('Exception occurred on insert: ' + e.getMessage()); }
        }       
    }
}

 
Leah SchneiderLeah Schneider
Figured it out.  It seems in no situations should you have a trigger on ContentDocument.  It needs to be on ContentVersion.  Something to do with order of operations even though it's less efficient.
This was selected as the best answer
System Admin 1038System Admin 1038
@Schneider I wrote a trigger on DocumentLink as before insert and changed the ShareType to "C" and end with this error as you Specified "INSUFFICIENT_ACCESS_OR_READONLY, Invalid sharing type C: [ShareType]" Can you specify or suggest a solution for this. Thanks
Leah SchneiderLeah Schneider
You need to use the process I specified.  You can't change that field in a trigger.