I am Facing this issue only while executing the below code in the Guest User context, how to resolve it?

The exeption occurs while inserting contentDocumentLink Record.

        ContentVersion cv = new ContentVersion();
        cv.title = filename; 
        cv.versionData = test.isRunningTest() ? Blob.valueOf('test body') : contentBody;
        cv.pathonclient = '/'+filename;
        cv.FirstPublishLocationId = [select isSandbox from Organization].isSandbox ? '0050l000003ertXAAQ' : '05898000045xtVAAQ';
        cv.SharingOption = 'A';
        insert cv;

        // create a new contentdocumentlink record to link the file to an record
        ContentDocumentLink cdl = new ContentDocumentLink();
        cdl.contentdocumentid = [SELECT ContentDocumentId FROM ContentVersion WHERE Id = :cv.Id].ContentDocumentId;
        cdl.linkedentityid = recordid; // set to the id of the record
        cdl.sharetype = 'v'; // set the sharing access to viewer
        cdl.visibility = 'allusers'; // set the visibility to all users in the organization
        insert cdl;


You must log in to answer this question.