[SalesForce] Error System.SObjectException: Business Account may not use Person Account field PersonHomePhone when loading Person Accounts using Test.LoadData

I get this error message:

System.SObjectException: Business Account may not use Person Account field PersonHomePhone

When I try and load Person Accounts using Test.LoadData() below is the data:

Name,FirstName,LastName,Phone,PersonHomePhone,PersonMobilePhone
GG,Graham,Green,,07970 123 123,
MB,Mary,Blue,,,07970 234 234

I followed the advice from a similar question, and try and change the Record Type Id after the load, but it never gets to this code:

    List<Account> personAccounts = new List<Account>();
    for (Account pa : accountList) {
        Account personAccount = new Account(Id = pa.Id, RecordTypeId = typeId);
        personAccounts.add(personAccount);
    }
    update personAccounts;

Is it possible to load Person Account fields using Test.LoadData() ?

Best Answer

For now, you have to specify the record type ID in your CSV:

Name,PersonHomePhone,RecordTypeId
"sfdc fox","123-456-7890",012500000009cP9

If you're doing a migration between non-related orgs, that means you'd also have to modify the static resource for each org, since the record type ID will be different.

There's an Idea you could vote for so that maybe we'll get the ability to use something else for record types, particularly the DeveloperName.


Side note: Technically, you're supposed to be able to create related records via Test.loadData's alternative multi-record process, but this only results in a gack (internal server error) for now:

    Test.loadData(new RecordType().getSObjectType(), 'personrecordtype');
    Test.loadData(Account.SObjectType, 'personaccounts');

personrecordtype

Id,Name,DeveloperName,SobjectType,IsPersonType,IsActive
1,Demo,DemoX1,Account,true,true

personaccounts

Name,PersonHomePhone,RecordTypeId
"sfdc fox","123-456-7890",012500000009cP9

I don't know when/if this would get fixed, but I'll see if I can find out more.

Class.System.Test.loadData: line 53, column 1
Class.TestLoadData.test1: line 4, column 1
08:06:38.0 (18283748)|FATAL_ERROR|System.UnexpectedException: Salesforce System Error: 299927037-4844 (-1054924158) (-1054924158)
Related Topic