4

I am trying to merge two person accounts in apex code using merge operation. I am getting the following error:

Merge failed. First exception on row 0 with id 001A000001AO7UdIAL; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on insert/update call: Name: [Name]

I suspect merge cannot be used on Person Accounts but cannot find any specific mention in the documents.

My question is "Can two Person Accounts be merged using DML merge operation?"

Thank you for your time.

I tried testing this in Anonymous block as below:

Account newAc7 = new Account();
newAc7.put('RecordTypeId', personAccountRecordTypeId);
newAc7.put('FirstName', 'TM27');
newAc7.put('LastName', 'Test7');
insert newAc7;        

Account newAc7a = new Account();
newAc7a.put('RecordTypeId', personAccountRecordTypeId);
newAc7a.put('FirstName', 'TM27a');
newAc7a.put('LastName', 'Test7a');
newAc7a.put('PersonOtherCity','The City');
newAc7a.put('Site','The site'); 
insert newAc7a;        

try {
    merge newAc7 newAc7a;
} catch (DmlException e) {
    System.debug('An unexpected error has occurred: ' + e.getMessage()); 
}

Account mA = [select Id, Name, PersonOtherCity, Site from Account where Id =: newAc7.Id][0];

system.debug('===mA======'+mA.Id+'========'+mA.PersonOtherCity+'========'+mA.Site);

The result is both accounts are created as person account, merge operation doesn't throw any errors, the loosing account is deleted after merge BUT the winning account doesn't get PersonOtherCity and Site values from loosing account.

Any Suggestions?

2

Check for following points before merging:

  • If your organization uses person accounts, both business accounts and person accounts are returned when you search for duplicates. However, you cannot merge a person account with a business account or vice versa.

  • You can't merge person accounts enabled to use a Customer Portal.

If it isn't the case, try to modify the default Account Record Type to a Business Account record type then merge.

Update: "Name" field is not writeable when you're attempting to create/update a Person Account record

Reference: https://help.salesforce.com/HTViewHelpDoc?id=account_merge.htm&language=en_US

  • I did run into this document while searching for an answer. This refers to the Merge Accounts option under Tools section on Accounts tab and not for merge using DML statement in APEX code. Thanks. – Custom Software Oct 1 '14 at 9:38
  • @CustomSoftware I believe this should apply on DML also. But in Apex there is a thing: Name field is not writable when you're attempting to create/update a Person Account record – Ashwani Oct 1 '14 at 9:50
  • I did verify my accounts against this. And none of the conditions apply. I am aware that Name field is readonly for person accounts, and that is why I am thinking if the merge operation is treating the Person accounts as Accounts and tries to modify the name field. And if that is the case how do I tell it these are person accounts and not accounts? – Custom Software Oct 1 '14 at 10:01
  • 1
    @CustomSoftware Can you remove the "Name" field from SOQL query you are using or if you are creating a new account leave that field non-assigned. Also , you can know the person account by "isPersonAccount" field on Account. – Ashwani Oct 1 '14 at 10:32
  • 1
    @CustomSoftware Can you try opposite of it: merge newAc7a newAc7; because field values on the master record always supersede the corresponding field values on the records to be merged. – Ashwani Oct 1 '14 at 15:19
0

Answer for your question ("Can two Person Accounts be merged using DML merge operation? ") is YES.

There are some points you must consider before doing merge operation :

  • You can only merge a person account with a person account.
  • Check first if the account is person account or not like this {!Account.IsPersonAccount}.
  • Get separate list of accounts you want to merge.
  • Perform DataBase.merge(winningList,aboutToMergeList,false).

Please refer to this link Click Here

Please mark this as an answer if this helps you :).

  • The document you have referred is for merge fields for email etc. templates not for merging records through apex DML. Yes, I am trying to merge Person Account to Person Account. Though not related to the question I have, I don't think the first parameter on database.merge can be a list of winning accounts, it should be a single winning account. Was your answer of YES based on your experience or the document you referred? – Custom Software Oct 1 '14 at 10:37
  • Please check this link help.salesforce.com/… this document says that you can merge person account. My bad about winning list. It should be the returnd sobject as a result of merge operation. What happened when you did this operation? – Eric Oct 1 '14 at 10:48
0

I realise that even nulls in the master record supersede the non-null values in record being merged. So the answer is that two person accounts can be merged YES. The error I have about name fields is something I will need to deal with by rectifying my code. Thank you all for your time.

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.