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

Cannot create PersonAccount in test class.



Below is my sample code for test class:


private class CtrlTourPlanningWithFilters_Test {
    static testMethod void CtrlTourPlanningWithFilters_PL_User_Test() {
        String RecTypeId= [select Id from RecordType where (Name='Person Account') and (SobjectType='Account')].Id;
Account Accnt = new Account(
 FirstName='Test FName',
 LastName='Test LName',
insert Accnt;
system.debug('@@@' + Accnt.isPersonAccount);
When I check the debuglog, the "isPersonAccount" is set to false. Am I doing something that is not right?

Hi Dane,


I'm not exactly sure the Offical answer but from my experience, when inserting a record, it will only return the ID.


If you want to return fields like Autonumber fields, formula's etc, you must request the information back after inert. I presume that IsPersonAccount would act the same way.


List<Account> CompleteAccountInfo = [SELECT Id,IsPersonAccount FROM Account WHERE Id=:Accnt.Id LIMIT 1]

SYSTEM.DEBUG('@@@' + CompleteAccountInfo[0].isPersonAccount);



I'd like to know why you are looking up if the record is person account? What do you need to do based on if the record is a person account?


Does your org only use Person Accounts?


Hi Dane,


As long as your ORG has person accounts enabled, and your sandbox has been refreshed with this, the below code should work.


List<Account> remRecords= [SELECT Id FROM Account];  
        DELETE remRecords;
        System.assertEquals(0, [SELECT count() FROM Account]);
        List<Account> Accounts = new List<Account>();      
        Account AC1 = NEW Account(

        INSERT Accounts;
        System.assertEquals(1,[SELECT count() FROM Account]);