1

I have a use case whereby I'm trying to convert an Account generated from the LiveChat pre-chat API into a Person Account. Some sample code is as follows. Note the Record Type names are arbitrary, with Generic representing an Account RT which corresponds to Business Accounts and Person_Account_RT corresponding to Person Accounts.

Account act = new Account();
act.Name = 'foobar';
act.ChatEmail__c = '[email protected]';
act.ChatFirstName__c = 'foo';
act.ChatLastName__c = 'bar';
act.RecordTypeId = getAccountRecordType('Generic').Id;
insert act;

Contact con = new Contact();
con.FirstName = 'foo';
con.LastName = 'bar';
con.Email = '[email protected]';
con.RecordType = getContactRecordType('Person_Account_RT');
con.AccountId = act.Id;
insert con;

act.RecordType = getAccountRecordType('Person_Account_RT');
update act;

act = [SELECT Id, IsPersonAccount, RecordType.Id FROM Account WHERE Id = :act.Id LIMIT 1];
System.debug(act);

System.assert(act.IsPersonAccount);

In this design I get no exception, but the Record Type is not set on the account and it does not convert to a Person Account.

If I change to use RecordTypeId instead as follows:

act.RecordTypeId = getAccountRecordType('Person_Account_RT').Id;
update act;

I am getting the following exception: System.DmlException: Update failed. First exception on row 0 with id 0010r00000ByjDiAAJ; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Cannot specify any additional fields when marrying or separating a Person-Account: []

1 Answer 1

4

The second code is closer to be accurate, but you "can't specify additional fields", so you actually need to do something like this:

Account convertToPersonAccount = new Account(
    Id=act.Id, 
    RecordTypeId=getAccountRecordType('Person_Account_RT')
);
update convertToPersonAccount;
act = [SELECT Id, IsPersonAccount, RecordType.Id FROM Account WHERE Id = :act.Id];
System.assert(act.IsPersonAccount);
1
  • Oh @sfdcfox, you are my bae!
    – Shanerk
    Nov 19, 2019 at 12:55

You must log in to answer this question.

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