0

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;
  • What is ld1? It's not declared here. Can you confirm which line throws the exception? – David Reed Nov 18 '19 at 17:42
  • Edited the original question. Added ld1 which is an instance of a custom object child of the Opportunity, multiple of these are created when an opportunity is inserted. The line throwing the error is the last line: 'insert cv3'. Thanks – Alex Cortes Nov 18 '19 at 18:32
  • What's the type of relationship between that object and Opportunity? What's the OWD on that object? – David Reed Nov 18 '19 at 19:01
  • It is a Master-Detail, the OWD is is "Controlled by Parent", the Community profile have Read,, Create, Edit over the parent object (Opportunity) – Alex Cortes Nov 18 '19 at 19:07
2

The community user should have access to Loan_Doc_Sort__c record to upload files on it.

You have few options,

  1. Change the OWD of Loan_Doc_Sort__c to public read-only external
  2. Create a manual share record for Loan_Doc_Sort__c after insert and share it with the commnunity user.
  3. Ensure The community user is the one creating the Loan_Doc_Sort__c in 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
    System.runAs(contactUser) {           
        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];
        ContentVersion cv3 = new ContentVersion();
        cv3.title = 'test content trigger';      
        cv3.PathOnClient ='test';           
        cv3.VersionData =Blob.valueOf('Test Content'); 
        cv3.FirstPublishLocationId =  ld1.Id;        
        insert cv3;
    }
    }
    
  • My external sharing settings were set to "private", created a sharing rule since I didn't want to modify the OWD. Thanks for the response. – Alex Cortes Nov 21 '19 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.