function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sfdcfoxsfdcfox 

INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY when using Person Account Record Type in testMethod

I have a test method that looks like the following:

 

 

@isTest
private class systemTest() {
static testMethod void runTest1() {
account a = new account(name='Test Person');
insert a;
contact c = new contact(firstname='Test', lastname='Person', accountid=a.id);
insert c;
RecordType rt = [select id from recordtype where sobjecttype = 'Account' and name = 'Test'];
a.recordtypeid = rt.id;
update a;
}
}

The wrinkle here is that the named RecordType is a person account record type. So, ultimately, what I am trying to do is, in a test method, create a person account and then run a test against it (the trigger in on the account object, as person accounts do not trigger contact triggers).

 

Does anyone have an idea why this might be happening? I do not see anything on the forums or in the documentation to explain this. The error occurs at the line in bold above. The error does not state which field is failing. However, if I remove the line"a.recordtypeid = rt.id", the code passes, but my trigger won't work without this bit of logic in place (it's designed to work specifically with person accounts).

 

Best Answer chosen by Admin (Salesforce Developers) 
IanRIanR

Hi,

 

To create a person account in code, you need to specify FirstName and LastName on the Account entity,

 

i.e.

 

Account a = new Account (FirstName='Joe', LastName='Bloggs'); insert a;

 

 

--> Will create a PersonAccount (and I believe will create the Contact record for you...)

 

but:

 

 

Account a = new Account (Name='Joe Bloggs'); insert a;

 

--> Will create a standard Account

 

hope this helps :)

All Answers

IanRIanR

Hi,

 

To create a person account in code, you need to specify FirstName and LastName on the Account entity,

 

i.e.

 

Account a = new Account (FirstName='Joe', LastName='Bloggs'); insert a;

 

 

--> Will create a PersonAccount (and I believe will create the Contact record for you...)

 

but:

 

 

Account a = new Account (Name='Joe Bloggs'); insert a;

 

--> Will create a standard Account

 

hope this helps :)

This was selected as the best answer
sfdcfoxsfdcfox

Talk about unintuitive. I thought it'd be just like the API in that regards. Well, it works, so I'm not complaining. Thanks!