Delete failed. first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []

1.9K    Asked by AnishaDalal in Salesforce , Asked on Jul 19, 2021

 I am trying to delete a share on a custom field, using a profile that doesn't have permissions for it.

field: Order_Request__C

And getting the following error:

System.DmlException: Delete[] failed. First exception on row 0 with id 02c5E00001RXlyMQAT; first error: INSUFFICIENT_ACCESS_OR_READONLY, insufficient access rights on object id: []

That's the function that deletes the sharing:

private void removeOrderRequestShare(Map> orderRequestIdToUsersSetMap, String accsessLevel) { List sharedToDropList = new List(); sharedToDropList = CustomSharedUtil.obtainOrderRequestShareToDrop(orderRequestIdToUsersSetMap, accsessLevel); delete sharedToDropList; }

That's CustomSharedUtil.obtainOrderRequestShareToDrop:

public static List obtainOrderRequestShareToDrop(Map> orderRequestIdToUsersSetMap, String accsessLevel) { List sharedToDropList = new List(); Set usersIdSet = new Set(); for (Id orderRequestId_i : orderRequestIdToUsersSetMap.keySet()) { usersIdSet.addAll(orderRequestIdToUsersSetMap.get(orderRequestId_i)); } List tmpSharedList = [SELECT Id, ParentId, UserOrGroupId FROM Order_Request__Share WHERE ParentId IN: orderRequestIdToUsersSetMap.keySet() AND UserOrGroupId IN: usersIdSet AND AccessLevel =: accsessLevel]; if (!tmpSharedList.isEmpty()) { for (Order_Request__Share share_i : tmpSharedList) { if (orderRequestIdToUsersSetMap.containsKey(share_i.ParentId) && orderRequestIdToUsersSetMap.get(share_i.ParentId).contains(share_i.UserOrGroupId)) { sharedToDropList.add(share_i); } } } System.debug('sharedToDropList: ' + sharedToDropList); return sharedToDropList; }

to fix it, i tried to add "without sharing" to the class, but it didnt work. This didn't work. i looked all over and ran out of ideas, please help. How to fix this delete[] failed issue.

Answered by Anurag Singhal

It's probably a hierarchy issue, as already mentioned. However, to actually delete the share will take a little trickery.

The answer is to use a webservice. Here is an example:

  global class ObjectUtilWebservice { webservice static Boolean deleteObjects(String[] ids, String objectType) { String query = 'SELECT Id FROM ' + objectType + ' WHERE Id IN :ids '; try { SObject[] objects = Database.query(query); delete objects; return true; } catch (Exception e) { return false; } } } 

This object takes a list of Ids and an object type and will delete anything you pass it.

  Eg: String[] ids = new String[]{'a0429000003G1OdAAK'}; ObjectUtilWebservice.deleteObjects(ids, 'YourSobjectName');

Now all you need to do is modify your original call to return a list of ids rather than objects and pass it to the web services to handle.

Note: There are no limits to checking here or any check to ensure that the caller has permission to delete these objects, so use carefully.



Your Answer

Interviews

Parent Categories