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
hector.asp2hector.asp2 

Merge not working on Contacts and Leads

Hi All,

 

I am trying to Merge Contacts and Leads using the code example given below for Accounts.

 

 

List<Account> ls = new List<Account>{new Account(name='Acme Inc.'),new Account(name='Acme')};
insert ls;
Account masterAcct = [select id, name from account where name = 'Acme Inc.' limit 1];
Account mergeAcct = [select id, name from account where name = 'Acme' limit 1];
try {
merge masterAcct mergeAcct;
} catch (DmlException e) {
// Process exception here 

}

 

The above code works but when I code on similar lines and change Account to Lead or Contact. I get the error:

Compile Error: Field is not writeable: Contact.Name at line 66.....

Here is what I tried:

List<Contact> ls = new List<Contact>{new Contact(name='Classic Inc.'),new Contact(name='Classic')};
insert ls;
Contact masterAcct = [select id, name from contact where name = 'Classic Inc.' limit 1];
Contact mergeAcct = [select id, name from contact where name = 'Classic' limit 1];
try {
merge masterAcct mergeAcct;
} catch (DmlException e) {
// Process exception here 

}

 

 

 

Thankjs

Hector...

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
jkucerajkucera

use FirstName and LastName instead of Name and it should work.  name is a merge field combinging the two (plus salutation & suffix).