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
exiaexia 

Cannot create PersonAccount in test class.

Hi, 

 

Below is my sample code for test class:

 

@isTest(seeAllData=true)
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(
 RecordTypeID=RecTypeId,
 FirstName='Test FName',
 LastName='Test LName',
 PersonMailingStreet='test@yahoo.com',
 PersonMailingPostalCode='12345',
 PersonMailingCity='SFO',
 PersonEmail='test@yahoo.com',
 PersonHomePhone='1234567',
 PersonMobilePhone='12345678' 
);
 
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?
 
Thanks,
Dane
zachbarkleyzachbarkley

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?

zachbarkleyzachbarkley

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(
            FirstName='John'
            ,LastName='Doe'
            ,PersonEmail='johndoe@test.com'

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