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
Markus PalloMarkus Pallo 

How to create personaccounts in apex test classes

Hello,

i need to test a class which needs account.isPersonAccount set.
My test fails, because i am not able to initialize the property inside the test method.

My test method:

@IsTest
    public static void testPersonAccountFields() {
        Schema.RecordTypeInfo recordTypeInfo = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByDeveloperName().get('PersonAccount');
        System.assert(recordTypeInfo != null, 'There must be a Person Account record type');
        String recordTypeId = recordTypeInfo.getRecordTypeId();
        Account account = new Account(RecordTypeId = recordTypeId);
        account.FirstName = 'First';
        account.LastName = 'Last';
        System.assert(account.isPersonAccount, 'must be a person account');
        // here call code which needs is PersonAccount set
    }

any help appreciated.

Markus
Best Answer chosen by Markus Pallo
Kritika RajKritika Raj
 To insert a Person Account we can write .
(Please mark this as best answer if it resolves your issue)
Id accRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Record Type Name').getRecordTypeId();

Account a = new Account();
a.FirstName = 'Test';
a.LastName = 'Person Account';
a.RecordTypeId = accRecordTypeId;

insert a;

Account a = [SELECT Id,IsPersonAccount FROM Account WHERE Id =: a.Id];
System.assertEquals(true, a.isPersonAccount);

All Answers

Markus PalloMarkus Pallo
I forgot to mention the assert fails, isPersonAccount is not set.
Kritika RajKritika Raj
 To insert a Person Account we can write .
(Please mark this as best answer if it resolves your issue)
Id accRecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Person Record Type Name').getRecordTypeId();

Account a = new Account();
a.FirstName = 'Test';
a.LastName = 'Person Account';
a.RecordTypeId = accRecordTypeId;

insert a;

Account a = [SELECT Id,IsPersonAccount FROM Account WHERE Id =: a.Id];
System.assertEquals(true, a.isPersonAccount);
This was selected as the best answer
Markus PalloMarkus Pallo

thanks Kritia Raj 

i have to requery than it will work, i changed to 

@IsTest
    public static void testPersonAccountFields() {
        Schema.RecordTypeInfo recordTypeInfo = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByDeveloperName().get('PersonAccount');
        System.assert(recordTypeInfo != null, 'There must be a Person Account record type');
        String recordTypeId = recordTypeInfo.getRecordTypeId();
        Account account = new Account(RecordTypeId = recordTypeId);
        account.FirstName = 'First';
        account.LastName = 'Last';
        insert account;
        account = [SELECT Id,IsPersonAccount FROM Account WHERE Id =: a.Id];
        System.assert(account.isPersonAccount, 'must be a person account');
        // here call code which needs is PersonAccount set
    }
Markus