Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. Request__Share reqShr = new Request__Share();
  2.  
  3. // Set the ID of record being shared.
  4. reqShr.ParentId = 'a3p1w00000008OJAAY';
  5.  
  6. // Set the ID of user or group being granted access.
  7. reqShr.UserOrGroupId = '0051w000000weEFAAY';
  8.  
  9. // Set the access level.
  10. reqShr.AccessLevel = 'Edit';
  11.  
  12. // Set rowCause to 'manual' for manual sharing.
  13. // This line can be omitted as 'manual' is the default value for sharing objects.
  14. reqShr.RowCause = Schema.Request__Share.RowCause.Has_Review__c;
  15.  
  16. // Insert the sharing record and capture the save result.
  17. // The false parameter allows for partial processing if multiple records passed
  18. // into the operation.
  19. Database.SaveResult sr = Database.insert(reqShr,false);
  20.  
  21. // Process the save results.
  22. if(sr.isSuccess()){
  23. // Indicates success
  24. system.debug('success');
  25. }
  26. else {
  27. // Get first save result error.
  28. Database.Error err = sr.getErrors()[0];
  29.  
  30. // Check if the error is related to trival access level.
  31. // Access level must be more permissive than the object's default.
  32. // These sharing records are not required and thus an insert exception is acceptable.
  33. if(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION &&
  34. err.getMessage().contains('AccessLevel')){
  35. // Indicates success.
  36. system.debug('success');
  37. }
  38. else{
  39. // Indicates failure.
  40. system.debug(err);
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement