0

I'm getting this exception when inserting to the Share table of a custom object. I've mirrored the code on document here: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm

Request__Share reqShr  = new Request__Share();

// Set the ID of record being shared.
reqShr.ParentId = 'a3p1w00000008OJAAY';

// Set the ID of user or group being granted access.
reqShr.UserOrGroupId = '0051w000000weEFAAY';

// Set the access level.
reqShr.AccessLevel = 'Edit';

// Set rowCause to 'manual' for manual sharing.
// This line can be omitted as 'manual' is the default value for sharing objects.
reqShr.RowCause = Schema.Request__Share.RowCause.Has_Review__c;

// Insert the sharing record and capture the save result. 
// The false parameter allows for partial processing if multiple records passed 
// into the operation.
Database.SaveResult sr = Database.insert(reqShr,false);

// Process the save results.
if(sr.isSuccess()){
    // Indicates success
    system.debug('success');
}
else {
    // Get first save result error.
    Database.Error err = sr.getErrors()[0];

    // Check if the error is related to trival access level.
    // Access level must be more permissive than the object's default.
    // These sharing records are not required and thus an insert exception is acceptable. 
    if(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION  &&  
            err.getMessage().contains('AccessLevel')){
    // Indicates success.
        system.debug('success');
    }
    else{
    // Indicates failure.
        system.debug(err);
    }
}

When I (System Admin) run this as Anonymous Apex I still get the same error. The Request__Share table's Org-Default is set as 'Private' and the user I'm trying to give access to isn't the current owner but they do have Read and Edit access to the Request__c table.

I can't find much information about this scenario and why I would get this error, especially running it as a System Administrator.

5
  • Is this code running in a class declared with sharing or without sharing? – David Reed Apr 26 '19 at 12:34
  • I was running it as Anonymous Apex so I'm actually not sure. I'm also getting the same issue when I try and use Flows to achieve the same result (kicked off by Process Builder which runs in System Mode). I'm also running as SysAdmin so I didn't think that would be an issue? – Chris Abraham Apr 26 '19 at 12:40
  • I've just made a wrapper class and tried with both 'with' and 'without' sharing and am getting the same issue. – Chris Abraham Apr 26 '19 at 12:48
  • Is the Request object is Child object of a Master-Detail relationship? – Santanu Boral Apr 26 '19 at 13:26
  • No and it's set to Private in the Org-wide defaults – Chris Abraham Apr 26 '19 at 13:35
0

I ran into similar kind of issue some time back. Please refer to my answer here.

If you are programatically trying to give a user, access to a record, to which it is already having access, then you would encounter this error.

Looks like, in your case user 0051w000000weEFAAY already has access to the record.

Have you tried running the below SOQL, to check what value rowCause field holds. If the query returns any result, rowCause will give you the reason how this record is shared.

Select Id, RowCause from Request__Share where UserOrGroupId = '0051w000000weEFAAY'
2
  • This isn't the case and that query returns no rows. The user does not have access to the record. If you check the code - this insert exception is explicitly handled so even if this was the case, it should be okay. – Chris Abraham Apr 29 '19 at 9:33
  • Have you changed the ParentID and UserOrGroupId in the copied code from the developer document? The UserOrGroupId starts with '00G' or '005' – Lokesh Kumar May 22 '20 at 2:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.