Professional Documents
Culture Documents
Sharing A Record Using Apex - Apex Developer Guide - Salesforce Developers
Sharing A Record Using Apex - Apex Developer Guide - Salesforce Developers
Sharing A Record Using Apex - Apex Developer Guide - Salesforce Developers
(/)
Language
English
objectNameAccessLevel The level of access that the specified user or group has been granted for a
share sObject. The name of the property is AccessLevel appended to the
object name. For example, the property name for LeadShare object is
LeadShareAccessLevel. Valid values are:
Edit
Read
All
This field must be set to an access level that is higher than the organization’s
default access level for the parent object. For more information, see
Understanding Sharing (atlas.en-
us.apexcode.meta/apexcode/apex_bulk_sharing_understanding.htm).
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm 1/8
4/24/2019 Sharing a Record Using Apex | Apex Developer Guide | Salesforce Developers
Property Name Description
RowCause The reason why the user or group is being granted access. The reason
determines the type of sharing, which controls who can alter the sharing
record. This field cannot be updated.
UserOrGroupId The user or group IDs to which you are granting access. A group can be:
A public group or a sharing group associated with a role.
A territory group.
You can share a standard or custom object with users or groups. For more information about the types of
users and groups you can share an object with, see User (https://developer.salesforce.com/docs/atlas.en-
us.218.0.object_reference.meta/object_reference/sforce_api_objects_user.htm) and Group
(https://developer.salesforce.com/docs/atlas.en-
us.218.0.object_reference.meta/object_reference/sforce_api_objects_group.htm) in the Object
Reference for Salesforce (https://developer.salesforce.com/docs/atlas.en-
us.218.0.object_reference.meta/object_reference/).
Manual shares written using Apex contains RowCause="Manual" by default. Only shares
with this condition are removed when ownership changes.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm 2/8
01
4/24/2019 public class JobSharing { a Record Using Apex | Apex Developer Guide | Salesforce Developers
Sharing
02
03 public static boolean manualShareRead(Id recordId, Id userOrGroupId){
04 // Create new sharing object for the custom object Job.
05 Job__Share jobShr = new Job__Share();
06
07 // Set the ID of record being shared.
08 jobShr.ParentId = recordId;
09
10 // Set the ID of user or group being granted access.
11 jobShr.UserOrGroupId = userOrGroupId;
12
13 // Set the access level.
14 jobShr.AccessLevel = 'Read';
15
16 // Set rowCause to 'manual' for manual sharing.
17 // This line can be omitted as 'manual' is the default value for sharing obj
18 jobShr.RowCause = Schema.Job__Share.RowCause.Manual;
19
20 // Insert the sharing record and capture the save result.
21 // The false parameter allows for partial processing if multiple records pas
22 // into the operation.
23 Database.SaveResult sr = Database.insert(jobShr,false);
24
25 // Process the save results.
26 if(sr.isSuccess()){
27 // Indicates success
28 return true;
29 }
30 else {
31 // Get first save result error.
32 Database.Error err = sr.getErrors()[0];
33
34 // Check if the error is related to trival access level.
35 // Access level must be more permissive than the object's default.
36 // These sharing records are not required and thus an insert exception is
37 if(err.getStatusCode() == StatusCode.FIELD_FILTER_VALIDATION_EXCEPTION &
38 err.getMessage().contains('AccessLevel')){
39 // Indicates success.
40 return true;
41 }
42 else{
43 // Indicates failure.
44 return false;
45 }
46 }
47 }
48
49 }
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm 3/8
01
4/24/2019 @isTest Sharing a Record Using Apex | Apex Developer Guide | Salesforce Developers
02 private class JobSharingTest {
03 // Test for the manualShareRead method
04 static testMethod void testManualShareRead(){
05 // Select users for the test.
06 List<User> users = [SELECT Id FROM User WHERE IsActive = true LIMIT 2];
07 Id User1Id = users[0].Id;
08 Id User2Id = users[1].Id;
09
10 // Create new job.
11 Job__c j = new Job__c();
12 j.Name = 'Test Job';
13 j.OwnerId = user1Id;
14 insert j;
15
16 // Insert manual share for user who is not record owner.
17 System.assertEquals(JobSharing.manualShareRead(j.Id, user2Id), true);
18
19 // Query job sharing records.
20 List<Job__Share> jShrs = [SELECT Id, UserOrGroupId, AccessLevel,
21 RowCause FROM job__share WHERE ParentId = :j.Id AND UserOrGroupId= :user2
22
23 // Test for only one manual share on job.
24 System.assertEquals(jShrs.size(), 1, 'Set the object\'s sharing model to Pri
25
26 // Test attributes of manual share.
27 System.assertEquals(jShrs[0].AccessLevel, 'Read');
28 System.assertEquals(jShrs[0].RowCause, 'Manual');
29 System.assertEquals(jShrs[0].UserOrGroupId, user2Id);
30
31 // Test invalid job Id.
32 delete j;
33
34 // Insert manual share for deleted job id.
35 System.assertEquals(JobSharing.manualShareRead(j.Id, user2Id), false);
36 }
37 }
The object’s organization-wide default access level must not be set to the most
permissive access level. For custom objects, this level is Public Read/Write. For more
information, see Understanding Sharing (atlas.en-
us.apexcode.meta/apexcode/apex_bulk_sharing_understanding.htm).
1 MyReasonName__c
1 Schema.CustomObject__Share.rowCause.SharingReason__c
For example, an Apex sharing reason called Recruiter for an object called Job can be referenced as
follows:
1 Schema.Job__Share.rowCause.Recruiter__c
Apex sharing reasons and Apex managed sharing recalculation are only available for
custom objects.
Under certain circumstances, inserting a share row results in an update of an existing share row. Consider
these examples:
A manual share access level is set to Read and you insert a new one set to Write. The original share
rows are updated to Write, indicating the higher level of access.
Users can access an account because they can access its child records (contact, case, opportunity, and
so on). If an account sharing rule is created, the sharing rule row cause (which is a higher access level)
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm
6/8
replaces
4/24/2019 the parent implicit share
Sharingrow cause,
a Record Usingindicating the higher
Apex | Apex Developer Guide |level of access.
Salesforce Developers
The object’s organization-wide default access level must not be set to the most
permissive access level. For custom objects, this level is Public Read/Write. For more
information, see Understanding Sharing (atlas.en-
us.apexcode.meta/apexcode/apex_bulk_sharing_understanding.htm).
Previous Next
(/docs/atla (/docs/atla
s.en- s.en-
us.apexcod us.apexcod
e.meta/ape e.meta/ape
xcode/apex xcode/apex
_bulk_shari _bulk_shari
ng_underst ng_recalc.h
anding.htm tm)
)
Get started
Heroku (https://devcenter.heroku.com/start)
Developer resources
Community
Developer Forums (/forums)
Webinars (/content/type/Webinar)
Learn more
Salesforce AppExchange (/appexchange)
© Copyright 2000-2018 salesforce.com, inc. All rights reserved. Various trademarks held by their respective owners.
Salesforce.com, inc. The Landmark @ One Market, Suite 300, San Francisco, CA 94105, United States
Feedback
About Us (http://www.salesforce.com/company/)
Language: English
(https://www.facebook.com/salesforcedevs) (https://twitter.com/#!/salesforcedevs)
(https://plus.google.com/118327959233932983591)
(https://www.linkedin.com/groups/Developer-Force-Forcecom-Community-3774731)
(https://www.youtube.com/user/DeveloperForce)
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_bulk_sharing_creating_with_apex.htm 8/8