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
Daan LuttikDaan Luttik 

Unexpected INVALID_FIELD_FOR_INSERT_UPDATE when merging contacts

Our goal is to merge Contacts (and later Accounts) in Salesforce CRM. Our data is already polluted therefore we can't rely on merge rules that are triggered only for new/changed contacts.

Therefore we think that we need to utilize Apex code however we are running into some issues that seem to have to do not with the code but with the permissions:
(Important to note about the code below, the emails in the contacts are fake and can therefore be ignored completely when merging).
public class MergeRule {
    public static void applyMergeRule() {
        AggregateResult[] contacts = [
            SELECT Name, BirthDate, COUNT(Email) nr
            FROM Contact GROUP BY Name, BirthDate
            HAVING COUNT(Email)>1
        ];
        for (AggregateResult contact_group: contacts) {
            String name = (String)contact_group.get('Name');
            Date birth_date = (Date)contact_group.get('BirthDate');
            Contact[] contact_subgroup = [
                SELECT Id, Name, BirthDate
                FROM Contact
                   WHERE Contact.Name = :name
                   AND Contact.BirthDate = :birth_date
						];
            for (Integer i=1; i<contact_subgroup.size(); i++){
								// This line produces the error 👇 🔔
                merge contact_subgroup[0] contact_subgroup[i];
            }
        }
    }
}

We receive the following error:
14:07:03:138 FATAL_ERROR System.DmlException: Merge failed. First exception on row 0 with id 0035r000002VYMoAAO; 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]
We have tried to change the field level security for the profile System Admin on the field Name for the Contacts object, however, we found that there is no way to change this field. We tried this due to the suggestions in this thread on the salesforce development forum (https://developer.salesforce.com/forums/?id=9060G0000005PvhQAE) and this thread on the salesforce stackexchange (https://salesforce.stackexchange.com/questions/69857/how-to-check-whether-a-user-has-write-access-to-a-field), however, we are not completely sure that we interpreted these threads correctly (maybe we need to change the permissions for other profiles, objects or fields instead of the ones above?)

I hope that you can help us with this issue.
Best Answer chosen by Daan Luttik
Daan LuttikDaan Luttik
For us, the issue, in the end, was that we retreived the contact not only with the Id but also with the Name and BirthDate.
When we changed the contact_subgroup query to the following it all worked:
Contact[] contact_subgroup = [
SELECT Id,
FROM Contact
WHERE Contact.Name = :name
   AND Contact.BirthDate = :birth_date
];

All Answers

ShirishaShirisha (Salesforce Developers) 
Hi Daan,

Greetings!

I can see that the error message is saying that the profile doesn't have the read acces for Name field.However,I would suggest you to check for all the fields included in the code.

Also,please make sure that you verify the user is system admin only.

Reference:https://help.salesforce.com/apex/HTViewHelpDoc?id=admin_fls.htm&language=en_us

Please mark it as best answer if it helps you to fix the issue.

Thank you!

Regards,
Shirisha Pathuri
Daan LuttikDaan Luttik
Hi Shirisha,

Thanks for the quick reply. May I ask how we can double-check that the code is run with the system admin user profile? For more context we are now running it in a sandbox mode (where I am fairly sure that the user has the system admin rights). But in the future, we want to run this as a timed job.
Jess RobinsonJess Robinson
Try splitting Name into FirstName and LastName. You can query the Name field but to update/write it, you have to use FirstName and LastName separately.
Daan LuttikDaan Luttik
For us, the issue, in the end, was that we retreived the contact not only with the Id but also with the Name and BirthDate.
When we changed the contact_subgroup query to the following it all worked:
Contact[] contact_subgroup = [
SELECT Id,
FROM Contact
WHERE Contact.Name = :name
   AND Contact.BirthDate = :birth_date
];
This was selected as the best answer