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
pucca0519pucca0519 

INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, when cloning notes & attachments

Hi -

 

In our instance, Account is set to private. We have sharing rules to share accounts by criteria. For example, grant read /write access to all account of AS record type with Role: AS. We load data using an application user named "Migration User" who is assigned to the System Administrator role. By default, all loaded accounts will be owned by the Migration User. We have a custom application that perform account merging. One of the steps is to clone and recreate Notes & Attachments because there isn't a way to re-parent these records to the master Account. When an AS user tries to merge two accounts, the cloning step is failing if there are Notes & Attachments owned by the Migration User.  This is the error that we're getting:

 

INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []. 

 

However, this same user can create new and/or edit existing Notes record owned by the Migration User.

 

Here is the snippet of code where notes are collected for cloning.

 

public static list<Note> getNoteToMerge(list<Account> masterList, List<Account> aList)
{
list<Note> nList = new list<Note>();

for(Note n : [SELECT Id, OwnerId, isPrivate FROM Note WHERE ParentId IN :aList])
{
/* query note and then clone it */
String idStr = 'id=' + '\'' + n.Id + '\'';
system.debug(LoggingLevel.Info, '***************************** idStr: ' + idStr);
String soql = getCreatableFieldsSOQL('note', idStr);
Note result = (Note)Database.query(soql);
Note cloneNote = result.clone(false, true, true, false);
cloneNote.ParentId = masterList[0].Id;
cloneNote.OwnerId = n.OwnerId;
cloneNote.isPrivate = n.isPrivate;
nList.add(cloneNote);
}
return nList;
}

 

--------------------------------------

 

// Returns a dynamic SOQL statement for the whole object, includes only creatable fields since we will be inserting a cloned result of this query
public static string getCreatableFieldsSOQL(String objectName, String whereClause){

String selects = '';

if (whereClause == null || whereClause == ''){ return null; }

// Get a map of field name and field token
Map<String, Schema.SObjectField> fMap = Schema.getGlobalDescribe().get(objectName.toLowerCase()).getDescribe().Fields.getMap();
list<string> selectFields = new list<string>();

if (fMap != null){
for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft)
Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd)
if (fd.isCreateable()){ // field is creatable
selectFields.add(fd.getName());
}
}
}

if (!selectFields.isEmpty()){
for (string s:selectFields){
selects += s + ',';
}
if (selects.endsWith(',')){selects = selects.substring(0,selects.lastIndexOf(','));}

}

return 'SELECT ' + selects + ' FROM ' + objectName + ' WHERE ' + whereClause;

}

 

Any input on how to fix this issue is greatly appreciated.

 

sandeep1989sandeep1989

make the owner of notes and attatchements as the owner of the account