Test Failed ‘Validation Errors While Saving Record(s)’ when loading Accounts with Test.LoadData

Background

When I use the Test.LoadData method to load Account records:

List<Account> accountList = (List<Account>) Test.loadData(Account.sObjectType, 'account_test_data');

I get this error message:

Validation Errors While Saving Record(s)

Even though there are no validation rules on the Account object.

The data is stored in a static resource with contentType of text/csv

FirstName,LastName,Phone,PersonHomePhone,PersonMobilePhone,RecordTypeId
Gary,Green,,07970 123 123,,0120O0000003bewQAA

And the debug log abruptly ends like this:

13:08:57.2 (3439995)|SYSTEM_METHOD_ENTRY|[1]|Test.Test()
13:08:57.2 (3445692)|STATEMENT_EXECUTE|[1]
13:08:57.2 (3469949)|SYSTEM_METHOD_EXIT|[1]|Test
13:08:57.2 (3494129)|METHOD_ENTRY|[5]||System.Test.loadData(Schema.SObjectType, String)

But when I create an Account using code:

Account a = new Account();
a.FirstName = 'Gary';
a.LastName = 'Green';
a.Phone = '';
a.PersonHomePhone = '07970 123 123';
a.PersonMobilePhone = '';
a.RecordTypeId = Id.valueOf('0120O0000003bewQAA');
insert a;

It works fine.

Question

Why am I getting the validation error when using Test.LoadData ?

Answer

There is currently a limitation with Test.LoadData in support for Person Accounts when attempting to set the RecordTypeId. This results in the error message:

Validation Errors While Saving Record(s)

If you do raise a support case for this it should reference back to W-7298675

Attribution
Source : Link , Question Author : Robs , Answer Author : Daniel Ballinger

Leave a Comment