There are different way of sharing a "private" object record in sfdc. Include using inherit role hierarchy ,sharing rule , or pressing custom button "Sharing" on record detail page in user interface. Codes below try to use apex API to do the same thing as the "Sharing" button. #Salesforce分享單筆資料代碼 // Create new sharing object for the custom object named MyCustomObject. MyCustomObject__Share myCustomObjectShr = new MyCustomObject__Share(); // Set the ID of record being shared. myCustomObjectShr.ParentId = recordId; // Set the ID of user or group being granted access. myCustomObjectShr.UserOrGroupId = userOrGroupId; // Set the access level. myCustomObjectShr.AccessLevel = 'Read'; // Set rowCause to 'manual' for manual sharing. // You may change to other row cause if you have created other sharing reason. myCustomObjectShr.RowCause = Schema.myCustomObject__Share.RowCause.Manual; // Insert the sharing record and capture the save result. Database.SaveResult sr = Database.insert(myCustomObjectShr,false); // Process the save results. if(sr.isSuccess()){ // Indicates success System.debug(LoggingLevel.INFO, 'Done'); } else{ // read first save result error. Database.Error err = sr.getErrors()[0]; System.debug(LoggingLevel.ERROR, 'Error code :' + err.getStatusCode() ); } |
No comments:
Post a Comment