How can you insert a Salesforce File with Collaborator ShareType?

I am currently trying to insert a Salesforce File attached to a custom Invoice object with the Collaborator (C) permission, as described on the Salesforce Documentation. The code was originally set to insert files with the Inferred (I) permission, which was causing issues when a user other than the file owner was trying to insert a new version of the file. Tracing the code I found the Domain in charge of creating the ContentDocumentLink object which is in charge of setting the permissions and ran into this method:

/* NOTE: This is the Salesforce ContentDocumentLink documentation, in order to set different
    share type permission, please follow ShareType field notation
    https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_contentdocumentlink.htm
*/
public ContentDocumentLink createNewDocumentLinkWithInferredPermission(SalesforceFileData inputFileData) {
    ContentDocumentLink contentDocumentLinkwithInferredPermission = super.associateFileToEntity(inputFileData);
    contentDocumentLinkwithInferredPermission.ShareType = 'I';
    return contentDocumentLinkwithInferredPermission;
}

Checked the super method and it had these details, which I believe should also be correct:

public virtual ContentDocumentLink associateFileToEntity(SalesforceFileData inputFileData) {
    ContentDocumentLink fileContentDocumentLink = new ContentDocumentLink(
        ContentDocumentId = inputFileData.contentDocumentId,
        LinkedEntityId = inputFileData.linkedSObjectId);
    return fileContentDocumentLink;
}

Modified the I to a C and ran a test to see if the fix would work as expected, but as soon as I ran the test code I got the following error:

Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_OR_READONLY, Invalid sharing type C: [ShareType]

This puzzled me for a moment, since it was clear on the documentation that you should be able to create such a ContentDocumentLink. Nevertheless I trucked on and try to find culprits that could be surrounding the issue, and ran into this post. I thought I might need that Visibility field to be populated. I changed the code one more time and ended up looking like this:

public ContentDocumentLink createNewDocumentLinkWithInferredPermission(SalesforceFileData inputFileData) {
    ContentDocumentLink contentDocumentLinkwithInferredPermission = super.associateFileToEntity(inputFileData);
    contentDocumentLinkwithInferredPermission.ShareType = 'C';
    contentDocumentLinkwithInferredPermission.Visibility = 'AllUsers';
    return contentDocumentLinkwithInferredPermission;
}

But I still got the same error. Kept trucking on and found myself into another post in regards to the permissions of sharing files. I started looking at the OWD in my org and found that the specific record type that I was trying to modify was in a private status. I thought that would do the trick, making so that the Internal users can share this files. No dice.

So this is where I am at now, not sure if someone can guide me through the process, or point out something I missed out in regards of how I am inserting the ContentDocumentLink. Just as clarification the process works with Inferred permissions, but it would not let other users submit new ContentVersions to the file if they are not the owner, which is a requirement on this task. Maybe there is an alternative to it that I don’t know about.

Answer

Attribution
Source : Link , Question Author : salesForceNoob , Answer Author : Community

Leave a Comment