+ Start a Discussion
Venkat TestVenkat Test 

Merge failed. First exception on row 0 with id 0031100000XXYfRAAX; first error: INSUFFICIENT_ACCESS_OR_READONLY, cannot merge with entity that is not accessible: []


Hi Guys,

I am trying to merge two contacts. Class is using the keyword  "without sharing". So the the class is running in system admin mode. I have checked the system admin profie, the profile is having all permissions(DELETE contact, MODIFY ALL DATA on contact).

I am calling this class on another class , that parent class is also using the keyword "without sharing". But still I am getting below error:
Merge failed. First exception on row 0 with id 0031100000XXYfRAAX; first error: INSUFFICIENT_ACCESS_OR_READONLY, cannot merge with entity that is not accessible: []

Below is class code:

public without sharing class DeleteContact {
    public void deleteContact(String contactId,string email){
        Map<String, Schema.SobjectField> contactfields = Schema.SObjectType.contact.fields.getMap();
        List<Schema.SObjectField> fldObjMapValues = contactfields.values();
        string theQuery1;
        string theQuery2;
        String theQuery = 'SELECT ';
            for(Schema.SObjectField s : fldObjMapValues)
            {
               String theLabel = s.getDescribe().getLabel();
               String theName = s.getDescribe().getName();
               theQuery += theName + ',';
            }
            theQuery = theQuery.subString(0, theQuery.length() - 1);
          
            theQuery1 = theQuery + ' FROM Contact WHERE Id =: contactId';
            theQuery2 = theQuery + ' FROM Contact WHERE Id !=: contactId AND Email =: email';
          
      
        Contact previouscontact=Database.query(theQuery1);
        List<Contact> listContacts=Database.query(theQuery2);
        if(previouscontact!=null && listContacts[0]!=null){
            try{
                merge  previouscontact listContacts[0];
            }
            catch(Exception e){
                system.debug('============='+e.getMessage());
            }
    }
}


Can some one please suggest me why this error is coming?. Urgent Please.
kibitzerkibitzer

The secret is in the following statement from the docs:

"This call requires that you decide prior to the merge call if there are any field values from the non-master record(s) that should supersede the values in the master record. If so, the field names and their new values should be set in the masterRecord of the MergeRequest, similar to a call to update."

If you set a field in the master record, the system will try to write that value into the merged result. You are setting all of the fields in the master record through the query - so it's trying to set all of those fields in the result - including readonly fields, which of course fails.

If you modify your code as follows:


String theQuery = 'SELECT ID, Email ';
            /*for(Schema.SObjectField s : fldObjMapValues)
            {
               String theLabel = s.getDescribe().getLabel();
               String theName = s.getDescribe().getName();
               theQuery += theName + ',';
            }*/
            theQuery = theQuery.subString(0, theQuery.length() - 1);

.. and not query all the fields, the problem will go away.

You should only query fields on the master record that you are going to want to manually set, and not query any readonly fields (except ID of course, which the system is smart enough to realize you're not trying to modify).
 

Venkat TestVenkat Test
Hi kibitzer,

Thanks for your reply.

I have changed the quesries as below:

Master record query:
Contact previouscontact=[Select Id,phone,email from contact where id=:contactId];

Child record query:
List<Contact> listContacts=[Select Id FROM Contact WHERE Id !=: contactId AND Email =: email];

But still getting the same error.

Is there any other thing , I am missing.

Thanks.

 
kibitzerkibitzer

Try posting your revised code? I implemented it as you describe and it works fine for me (I had previously reproduced your error condition).
 

Dan
 

Gonzalo AbrunaGonzalo Abruna
Most probably the issue is that the logged in user is not above the Owner of the Contact in the Role Hierarchy, or that the running user does not have Delete Contact permissions. In this case the without sharing notation is not very useful.


Regards,

Gonzalo.
Akram GargouriAkram Gargouri
For my case, contact merged had associated Portal User, so I used to add "Manage User" permissions to the Profile of User running the merge and my problem was solved