6

Question

In an org with Person Accounts enabled, it possible to use Test.loadData() to load test Account records from a .csv file as Person Accounts (not as Business Accounts), but without hardcoding their RecordTypeIds in the CSV?


EDIT: So far, I've experimented with a few solutions, to no avail.

Attempt 1: Loading the Account records from a CSV with only the following three columns: Id, FirstName, LastName

When I did this, I was unable to replicate the behavior detailed in Inserting person accounts in test mode in which Salesforce automatically knows what type of Account to create based on what fields are populated. Upon calling Test.loadData(), I received an error stating that the required field Name was missing, which I believe corresponds to Business Accounts.

When I added the fourth column, Name, to the existing csv as an experiment, calling Test.loadData() successfully inserted the Account records, but as expected, they were Business Accounts, and the FirstName and LastName fields on the inserted Accounts were null.

Attempt 2: Loading the Account records from a csv with no RecordTypeIds, then later dynamically assigning RecordTypeIds through Apex.

In addition to the Account CSV (with four columns, including Name, since I can't get around the Name required field error), I loaded Contacts from a CSV with columns: Id, AccountId, FirstName, LastName, such that each Contact only looked up to one Account, and no Contact looked up to the same Account.

In a test setup method, after loading the Accounts and Contacts from their respective Static Resource CSVs, I queried for one Person Account Record Type. Then, I iterated through the list of loaded Accounts, and assigned the Id of the queried Record Type to each Account's RecordTypeId. I assigned no other values to any other field on any other object.

Then, I called a DML update on the list of Account records. That is when I receive the error: Cannot specify any additional fields when marrying or separating a Person-Account: []

Attempt 3: Adding the IsPersonAccount column to the CSV, with values true.

The loaded records had IsPersonAccount set to true, but they were created with a RecordTypeId corresponding to Business Accounts.

  • 2
    Could you load the Account records from the static resource without specifying the RecordTypeId and then dynamically assign the correct ID afterwards in Apex? This was suggested in Importing RecordType as test data with Test.loadData() – Daniel Ballinger Apr 9 '18 at 21:31
  • 2
    Another interesting suggestion in Inserting person accounts in test mode. As long as the user has access to the Person Account record type then setting the FirstName and LastName fields should imply a Person Account and assign the correct record type. – Daniel Ballinger Apr 9 '18 at 21:36
  • 1
    Thanks for the responses, @DanielBallinger! Since posting the question, I've tried both: (1) loading the Account records from the CSV without specifying the RecordTypeId and dynamically assigning the ID to each record in Apex, and (2) loading the Account records from the CSV with FirstName and LastName columns; both to no success. (Unsure if it has something to do with config settings in the environment I'm working with, but I will update my question with the solutions I tried and the error messages I received.) – Angeline Tamayo Apr 9 '18 at 21:50
  • 2
    If you can't find a way to make this work... perhaps you could prepare some Person Accounts as JSON instead. Then you can query its body, deserialize and insert in just a few lines – Charles T Apr 9 '18 at 22:44
  • 2
    For Attempt 2 and the message Cannot specify any additional fields when marrying or separating a Person-Account. Try having only the RecordTypeID and the ID populated on the Account that is being updated. You could either create a new Account instance or strip out any other populated fields. – Daniel Ballinger Apr 10 '18 at 22:15
1

As per the comment discussion, start from the suggested answer in Importing RecordType as test data with Test.loadData().

This corresponds to Loading the Account records from a csv with no RecordTypeIds, then later dynamically assigning RecordTypeIds through Apex.

As you found, if you attempt to directly assign the RecordTypeIds on the records that come out of Test.loadData() you end up with the error:

Cannot specify any additional fields when marrying or separating a Person-Account: []

This can be worked around by having only the RecordTypeID and the ID populated on the Account that is being updated.

E.g.

RecordType personAccountRecordType = [Select Id from RecordType where ...];

List<sObject> personAccountsLoaded = Test.loadData(Account.sObjectType, 
                                    'personAccountDataWithoutRecordTypeIds');
List<Account> personAccounts = new List<Account>();
for(sObject pa : personAccountsLoaded) {
    Account personAccount = new Account(Id = pa.Id);
    personAccount.RecordTypeId = personAccountRecordType.Id;
    personAccounts.add(personAccount);
}
update personAccounts;
  • I am unable to change the record type, because I get an error when loading the CSV data: System.SObjectException: Business Account may not use Person Account field PersonHomePhone - what should I do to get around this? – Robs Oct 29 '18 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.