6

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 ?

16
  • I tried same code, got same error... Commented Oct 29, 2018 at 17:12
  • Do you have any other records that would be created as a result of creating an Account record? Do you receive the same validation error if you try and create an account with the same details through the UI manually? What is the validation error you are receiving?
    – Tad
    Commented Oct 29, 2018 at 18:13
  • 2
    Set your debug logs as high as they go (FINEST for all categories) in the Developer Console, run the tests from the Developer Console, you should see the validation errors in the logs from the test run.
    – sfdcfox
    Commented Oct 29, 2018 at 19:16
  • 1
    @sfdcfox i replicated it in newly created scratch org, It works via anon window and does work when i use loadData in test. Could be a bug, Robs if you have premium support can you raise it with Salesforce Support team? Commented Oct 29, 2018 at 19:21
  • 1
    @sfdcfox here is the GIST : gist.github.com/pranayjswl007/d817ef256a93751003918f861d9df45f Commented Oct 29, 2018 at 20:10

2 Answers 2

1

I don't know why but changing the RecordTypeId value in your csv file to 15 characters instead of 18 makes it work.

0120O0000003bewQAA ===> 0120O0000003bew
1
  • No idea why, but this worked for me
    – dzh
    Commented Aug 30, 2022 at 7:05
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .