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
Constance Rouge 8Constance Rouge 8 

Merge person account - issue with Name

Hi Everyone,

I have an apex class that merges two person accounts in this piece of code (knowing all the accounts in the org are person accounts, so I am sure the result of the queries isn't a business account):
Account masterAcct= [SELECT Id, personEmail, Name FROM Account WHERE PersonEmail= :leadToConvertEmail LIMIT 1];
Account newAcct = [SELECT Id, PersonEmail, Name FROM Account WHERE Id= :acctIdToCheck LIMIT 1];

merge masterAcct newAcct;

But I get this error: 
INVALID_FIELD_FOR_INSERT_UPDATE, Account: bad field names on insert/update call: Name: [Name]

If I change my code and delete Name (wether I add FirstName and LastName in the query or not):
Account masterAcct= [SELECT Id, personEmail FROM Account WHERE PersonEmail IN :leadsEmailList LIMIT 1];
Account newAcct = [SELECT Id, PersonEmail FROM Account WHERE Id= :acctIdToCheck LIMIT 1]; 

//Same result with: Account masterAcct= [SELECT Id, personEmail, salutation, firstName, lastName FROM Account WHERE PersonEmail IN :leadsEmailList LIMIT 1];
//And: Account newAcct = [SELECT Id, PersonEmail, salutation, firstName, lastName FROM Account WHERE Id= :acctIdToCheck LIMIT 1];

merge masterAcct newAcct;
I get the following error:
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: Account.Name

Therefore, is there a way to use merge with two person accounts?

Thanks or your help!

Constance
Constance Rouge 8Constance Rouge 8
I forgot to precise there is some piece of code between the queries, so it looks more like:
Account masterAcct= [SELECT Id, personEmail, Name FROM Account WHERE PersonEmail= :leadToConvertEmail LIMIT 1];

//Piece of code including the creation of an account and getting its id in the variable acctIdToCheck

Account newAcct = [SELECT Id, PersonEmail, Name FROM Account WHERE Id= :acctIdToCheck LIMIT 1];

merge masterAcct newAcct;

 
suman kumar 59suman kumar 59
Include FirstName,LastName,MiddleName, in your SOQL query this should resolve the issue