function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Michael MMichael M 

Error when trying to merge person accounts

Hello, I'm trying to merge 2 person accounts, and getting an error. The error is this:

"Line: 21, Column: 1
System.DmlException: Merge failed. First exception on row 0 with id 0016300000lyQrIAAU; first error: INVALID_FIELD_FOR_INSERT_UPDATE, Unable to create/update fields: Name. Please check the security settings of this field and verify that it is read/write for your profile or permission set.: [Name]"


The code is this:
         //** Tweak your account selection ... aka where clause ..
List<AggregateResult> MyMergeList = [SELECT Count(ID) , name, patient_dob__c
FROM Account 
WHERE IsPersonAccount=TRUE
and name like '%James Dean%'
GROUP BY Name, Patient_DOB__c
HAVING Count(ID) > 1 
ORDER BY Count(ID) desc
LIMIT 2000];


For (AggregateResult Agm : MyMergeList){
   //** Tweak the string parameters to match your preference (confidence)....
   //String accName = '%'+ string.valueOf(agm.get('Name')) + '%';
   String accName = string.valueOf(agm.get('Name'));
   Account ToAccount = [Select id, name from account where name like :accName limit 1];
   Account FromAccount = [Select id, name from account where Id != :ToAccount.Id and name like :accName Limit 1];

   system.debug(LoggingLevel.Info,'*** FromAccount: ' + FromAccount.Name+' ('+FromAccount.Id+')');
   system.debug(LoggingLevel.Info,'*** ToAccount  : ' + ToAccount.Name+' ('+ToAccount.Id+')');
   database.merge(ToAccount , FromAccount );
}
 
Best Answer chosen by Michael M
Michael MMichael M
I was able to make it work but using firstname instead of name in the 2 account queries:

 //** Tweak your account selection ... aka where clause ..
List<AggregateResult> MyMergeList = [SELECT Count(ID) , name, patient_dob__c
FROM Account 
WHERE IsPersonAccount=TRUE
and name like '%James Dean%'
GROUP BY Name, Patient_DOB__c
HAVING Count(ID) > 1 
ORDER BY Count(ID) desc
LIMIT 2000];


For (AggregateResult Agm : MyMergeList){
   //** Tweak the string parameters to match your preference (confidence)....
   //String accName = '%'+ string.valueOf(agm.get('Name')) + '%';
   String accName = string.valueOf(agm.get('Name'));
   Account ToAccount = [Select id, firstname, lastname from account where name like :accName limit 1];
   Account FromAccount = [Select id, firstname, lastname from account where Id != :ToAccount.Id and name like :accName Limit 1];

   system.debug(LoggingLevel.Info,'*** FromAccount: ' + FromAccount.firstname+' ('+FromAccount.Id+')');
   system.debug(LoggingLevel.Info,'*** ToAccount  : ' + ToAccount.firstname+' ('+ToAccount.Id+')');
   database.merge(ToAccount , FromAccount );
}

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Michael,

Greetings!

Can you please double check the FLS of the Name field for the affected user's profile or permission set which has been assigned for the user as per the error message.

Kindly mark it as best answer if it helps so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Michael MMichael M
Hi Shirisha,
The field level security is set to "Visible" for my profile. I also have modify all permission on the account object and edit access on the name field.
Michael MMichael M
I was able to make it work but using firstname instead of name in the 2 account queries:

 //** Tweak your account selection ... aka where clause ..
List<AggregateResult> MyMergeList = [SELECT Count(ID) , name, patient_dob__c
FROM Account 
WHERE IsPersonAccount=TRUE
and name like '%James Dean%'
GROUP BY Name, Patient_DOB__c
HAVING Count(ID) > 1 
ORDER BY Count(ID) desc
LIMIT 2000];


For (AggregateResult Agm : MyMergeList){
   //** Tweak the string parameters to match your preference (confidence)....
   //String accName = '%'+ string.valueOf(agm.get('Name')) + '%';
   String accName = string.valueOf(agm.get('Name'));
   Account ToAccount = [Select id, firstname, lastname from account where name like :accName limit 1];
   Account FromAccount = [Select id, firstname, lastname from account where Id != :ToAccount.Id and name like :accName Limit 1];

   system.debug(LoggingLevel.Info,'*** FromAccount: ' + FromAccount.firstname+' ('+FromAccount.Id+')');
   system.debug(LoggingLevel.Info,'*** ToAccount  : ' + ToAccount.firstname+' ('+ToAccount.Id+')');
   database.merge(ToAccount , FromAccount );
}
This was selected as the best answer