0

I need to create a Patient_Registration__c (which is a Person Account) under a specific user in my test class. Following is my code in the test class:

Profile p = [SELECT Id, Name FROM Profile WHERE Name='website Profile' LIMIT 1];
User u = [SELECT Id, Name FROM User WHERE ProfileId = :p.Id LIMIT 1];
TestFactory factory = new TestFactory();

    for(Integer i=0; i<5; i++) {

        factory.insertAllInterceptRegistration('Name'+i, u);       
    }

and following is the part in the TestFactory (which is just a helper class):

 public void insertAllInterceptRegistration(String firstName, User u) {

    this.createPatient(false);
    this.patient.FirstName = firstName;
    insert this.patient;

    Account a = [SELECT Id, FirstName, LastName, PersonContactId, CreatedDate, CreatedById FROM Account WHERE FirstName = :firstName];        

    system.debug('a >>> '+ a);      

    System.runAs(u) {

        Patient_Registration__c pr = this.createPatientRegistration(false, a.PersonContactId);
        pr.RecordTypeId = SObjectType.Patient_Registration__c.getRecordTypeInfosByName().get('Test').getRecordTypeId();
        pr.Opt_in_to_Receive_Information__c = 'yes';
        insert pr;
        Test.setCreatedDate(pr.Id, DateTime.now().addDays(-1));
    }
}

I'm creating a Person Account by calling createPatient method then I make my changes and insert. Then I query the account in order to retrieve the PersonContactId, here I runAs(u) which is my desired user to create the Patient_Registration__c account whith it, so I pass PersonContactId to createPatientRegistration (which creates a Patient_Registration__c Person Account). Then I make my changes and insert.

and I get the following error:

System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []

Class.TestFactory.insertAllInterceptRegistration: line 79, column 1 Class.InterceptRegistrationDataFeedTest.myUnitTest: line 30, column 1

Line 79 in TestFactory is the following:

insert pr;

Thank you in advance for your help! Cheers!

  • 2
    Does that user have access to that record type? – Adrian Larson Sep 21 '18 at 22:54
  • Hi @AdrianLarson thanks for your quick reply. Yes the user has access to that record type. Because in the actual batch class which I'm creating this test for, the user is able to perform all DML operations on the object. – Bahman.A Sep 22 '18 at 0:24
1

The only thing I can see that would give you an INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY error is if User u doesn't have access to Account a. Try changing the owner of the account to that user before the runAs block and see if that solves the problem.

As an aside, it seems like you're using seeAllData=true in your test class. Is that right? It would be better for you to create and insert the Account record in the test class, instead of relying on actual data in the database.

  • Hi @Shane Steinfeld, Thank you for your answer. The user DOES have access to Account. And I DON'T have seeAllData=true. – Bahman.A Sep 28 '18 at 19:30
  • @Bahman.A -- hmm. Can you show us the createPatientRegistration method? Also, if you write pr out to the debug log just before your insert pr statement, what does it show? – Shane Steinfeld Sep 28 '18 at 23:09

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.