I'm unable to create ContentVersion records running a test class as a Partner Community user. I have created a Partner user using the same methods displaying in the test class, tested the process manually and works properly. It is annoying since it's only failing in the test class. I'm using a public class and I have granted access to all the involved objects and fields.
Error:
System.DmlException: Insert failed. First exception on row 0; first
error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, You do not have
the level of access necessary to perform the operation you requested.
Please contact the owner of the record or your administrator if access
is necessary.: [NetworkId]
Fragment of the Test Class:
static testMethod void uploadFiles() {
//Creating Broker Data
Account partnerAcc = TestDataFactoryCls.CreateAccount();
partnerAcc.IsPartner = true;
update partnerAcc;
Contact partnerCon = TestDataFactoryCls.CreateContact(partnerAcc.Id);
User contactUser = TestDataFactoryCls.createUserFromContact(partnerCon, 'Partner Community Clone');
//Creating Data Owned by the Broker
Account acct = TestDataFactoryCls.CreateAccount();
Contact contact = TestDataFactoryCls.CreateContact(acct.Id);
Opportunity opp1 = TestDataFactoryCls.CreateOpportunity(acct.Id, contact.Id,'fixed investor v5', 'Asset', 'Purchase', 'Closed Won', partnerCon.Id, partnerAcc.Id);
Opportunity opp2 = TestDataFactoryCls.CreateOpportunity(acct.Id, contact.Id,'fixed investor v5', 'Asset', 'Refi', 'Application', partnerCon.Id, partnerAcc.Id);
System.debug('opp2 before ' + opp2.StageName + ' ' + opp2.AFS_Referrer_Name__c + ' ' + opp2.Broker_Contact__c);
List<Loan_Doc_Sort__c> lds = [SELECT Id, Loan_Documentation__c, Loan__c, Document__c, Comment_History__c, Comment__c, Status__c FROM Loan_Doc_Sort__c WHERE Loan__c = :opp2.Id];
Loan_Doc_Sort__c ld1 = lds[0];
System.runAs(contactUser) {
ContentVersion cv3 = new ContentVersion();
cv3.title = 'test content trigger';
cv3.PathOnClient ='test';
cv3.VersionData =Blob.valueOf('Test Content');
cv3.FirstPublishLocationId = ld1.Id;
insert cv3;
}
}
Best Answer
The community user should have access to
Loan_Doc_Sort__c
record to upload files on it.You have few options,
Loan_Doc_Sort__c
to public read-only externalLoan_Doc_Sort__c
after insert and share it with the commnunity user.Ensure The community user is the one creating the
Loan_Doc_Sort__c
in the test class