Inserting person accounts in test mode

Hello My envrionment is using personAccounts. While I was developing my code I was running a testing method so my code will insert a person account. To my surprise, under this testing process after the insert command was applied, the respective inserted record showed an ID but the isPersonAccont value was FALSE. not only that, the record type was giving a null value and this was properly assigned using the command

 RecordType personAccountRecordType =  [SELECT Id FROM RecordType WHERE Name = 'Person Account' and SObjectType = 'Account'];

If I run the same code out of the testing environment then that record is properly inserted in the database with the mentioned fields isPersonAccount and RecordType assigned to their proper values (true and 'person Account' respectively). Any solution to have this working as expected in testing environment?

Thanks Jose

  • 1
    When you say in "testing environment" do you mean when running tests, or do you mean Production vs Sandbox? It would be helpful if you could post the code you are using.
    – dBeltowski
    May 10, 2016 at 15:03
  • 1
    In my org when creating a basic PersonAccount all we do is set FirstName and LastName. Account personAccount = new Account(FirstName = 'TestFirst', LastName = 'TestLast');
    – dBeltowski
    May 10, 2016 at 15:04
  • 1
    Fyi, in some code written by a colleague, the name used to query the record type ID is 'PersonAccount' not 'Person Account'. Code explicitly sets this record type ID.
    – Keith C
    May 10, 2016 at 15:20
  • 1
    I suspect that something is amiss with your unit test code. Perhaps you should edit your question and provide an example?
    – sfdcfox
    May 10, 2016 at 15:34

1 Answer 1


Based on dBeltowski's Comment I just solved this problem with the following insight:

  • If you want to create a Business Account in Testmode, just fill in the Name field
  • If you want to create a Person Account in Testmode, just fill in FirstName / LastName

If you want to set PersonAccount-Fields and BusinessAccount-Fields at the same time, the system (obviously) throws an exception.

The system will automatically realize what type of account it should create

  • In addition, check that the user being used for testing has access to the person account record type, otherwise you'll get this error: "entity type cannot be inserted: Person Account"
    – Matt Lacey
    Apr 9, 2018 at 3:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .