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

We're undertaking a process in which we are trying to convert Business Accounts/Contacts to Person Accounts.

When I run my code I am getting the following error:

INVALID_FIELD_FOR_INSERT_UPDATE, Cannot specify any additional fields when marrying or separating a Person-Account:

This seems to be a similar issue as posted here, however the resolution to that seemed to be to ONLY change the RecordType of the Account. I am already only changing that record type.

Code is here.

Savepoint sp = Database.setSavePoint();

Map<Contact, Account> contactToAccountMap = new Map<Contact, Account>();
Set<Id> contactIds;

try
{

    for(Contact c : [SELECT Id, AccountId, FirstName, LastName, MailingStreet, MailingCity, MailingState, MailingPostalCode, MailingCountry, Phone, (SELECT Id FROM OpportunityContactRoles) FROM Contact WHERE Account.RecordTYpe.Name IN ('Business Account 1', 'Business Account 2') AND ID NOT IN (SELECT ContactId FROM OpportunityContactRole)])
    {
        Account a = createAccountFromContact(c);
        contactToAccountMap.put(c, a);
    }
    insert contactToAccountMap.values();

    for(Contact c : contactToAccountMap.keySet())
    {
        c.AccountId = contactToAccountMap.get(c).Id;
    }
    update new List<Contact>(contactToAccountMap.keySet());

    for(Account a : contactToAccountMap.values())
    {
        a.RecordTypeId = DatabaseUtils.getRecordTypeId('Account', 'Person Client');
    }
    update contactToAccountMap.values();
    Map<Id, Contact> tempMap = new Map<Id, Contact>(new List<Contact>(contactToAccountMap.keySet()));
    contactIds = tempMap.keySet();

}catch(Exception e)
{
    System.debug(e);
    Database.rollback(sp);
}finally
{
    //System.debug( JSON.serialize(  contactIds  ) );
    CustomEmail.sendSinglePlainTextEmail(Constants.ADMIN_EMAIL, Constants.ADMIN_EMAIL, 'Contact Convert', 'Contact IDs', String.join(new List<Id>(contactIds), '\n') );
}



private Account createAccountFromContact(Contact c)
{
    return new Account
            (
                Name = c.FirstName +' '+ c.LastName,
                Phone = c.Phone,
                BillingStreet = c.MailingStreet,
                BillingCity = c.MailingCity,
                BillingState = c.MailingState,
                BillingPostalCode = c.MailingPostalCode,
                BillingCountry = c.MailingCountry,
                RecordTypeId = DatabaseUtils.getRecordTypeId('Account', 'Business Client 1')
            );
}

The problem is happening when I try to update my Accounts after I've changed the Record Type.

for(Account a : contactToAccountMap.values())
{
    a.RecordTypeId = DatabaseUtils.getRecordTypeId('Account', 'Person Client');
}
update contactToAccountMap.values();

Best Answer

You still have the other fields specified... It doesn't matter that they've changed. Create new records in memory instead:

for(Contact c : contactToAccountMap.keySet()) { 
    Account a = contactToAccountMap.get(c);
    contactToAccountMap.put(c, new Account(Id=a.Id, RecordTypeId= DatabaseUtils.getRecordTypeId('Account', 'Person Client')));
} 
update contactToAccountMap.values();
Related Topic