Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement