1

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

field: Order_Request__C

i'm receiving 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<Id, Set<Id>> orderRequestIdToUsersSetMap, String accsessLevel) {
    List<Order_Request__Share> sharedToDropList = new List<Order_Request__Share>();

    sharedToDropList = CustomSharedUtil.obtainOrderRequestShareToDrop(orderRequestIdToUsersSetMap, accsessLevel);


    delete sharedToDropList; 

}  

That's CustomSharedUtil.obtainOrderRequestShareToDrop:

public static List<Order_Request__Share> obtainOrderRequestShareToDrop(Map<Id, Set<Id>> orderRequestIdToUsersSetMap, String accsessLevel) {

    List<Order_Request__Share> sharedToDropList = new List<Order_Request__Share>();
    Set<Id> usersIdSet = new Set<Id>();

    for (Id orderRequestId_i : orderRequestIdToUsersSetMap.keySet()) {
        usersIdSet.addAll(orderRequestIdToUsersSetMap.get(orderRequestId_i));
    }

    List<Order_Request__Share> 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.

1
  • What is user license of profile which is performing this operation?
    – Ayub
    Commented May 1, 2018 at 13:10

2 Answers 2

1

This issue I guess is the duplicate, the same is discussed here: Issue deleting object

Stating the reason that might be causing this: This was related to the role hierarchy. Since the users where on the same level in the role hierarchy the sharing rule would only give edit access and not delete access. To get delete access to a record you need to be either record owner, have "modify all data" or be over the user in the role hierarchy.

Read more here under the "User Managed Sharing, also known as Manual Sharing" section:

https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_bulk_sharing_understanding.htm

0

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 webservice to handle.

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

3
  • Nice.....How did you figure this one out? Also, why does it work? Commented Oct 5, 2022 at 15:54
  • Hmmm not sure. This was a while ago. I guess because it might remove you from the standard context. However... these days I'd probably use a platform event hooked up an update to truly remove context. Commented Oct 5, 2022 at 18:50
  • Nice. TIL- you can run a platform events as a specific user developer.salesforce.com/docs/atlas.en-us.platform_events.meta/… Commented Oct 7, 2022 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .