Greetings, everyone! In today's session, we'll delve into the topic of sharing records within an Apex class. As we're aware, there exist various methods through which we can accomplish the sharing of records.
We engage in record sharing primarily when the object's Organization Wide Default (OWD) settings are set to private. Sharing settings come into play when certain predefined criteria are met, allowing us to extend access to records to designated groups or roles.
In cases where intricate logic is involved, manual sharing is employed. While this approach proves beneficial for specific records, instances where a multitude of records require automated handling, Apex sharing becomes the preferred solution.
Salesforce offers a 'Share' object for each type of object, with distinct naming conventions:
For standard objects, it's 'StandardobjectName+Share', such as 'AccountShare' for the 'Account' object.
Custom objects follow the pattern 'CustomObjectName__Share', like 'Custom_Object__Share' for the 'Custom_Object__c'.
Sharing Custom Object Fields:
ParentId: Represents the ID of the record you intend to share.
UserOrGroupId: Corresponds to the ID of the user, public group, or roles with whom you aim to share the record.
AccessLevel: Denotes the level of access granted to the specified user or group. It can be Read, Edit, or All. The 'All' permission is exclusively granted by the system, making it view-only without the ability to assign through code.
RowCause: Signifies the rationale behind sharing, which can provide insight when reviewing the shared record's justification.
Apex Sharing Reasons:
Before delving into the creation of any Apex-managed sharing code, the initial step involves establishing an Apex Sharing Reason. To initiate this process:
1.Navigate to Setup > Create > Objects.
2.Choose the relevant custom object. (For instance, the "Test" Custom object in this scenario.)
3.Within the Apex Sharing Reasons-related list, select the 'New' option.
4.Provide a label for the designated Apex Sharing Reason.
5.Assign a name to uniquely identify the Apex Sharing Reason.
6.Finalize by clicking the 'Save' button.
1.Sharing a standard object record
public
class
ShareStandardObjectRecord{
public
static
void
shareRecord(Id accountId){
AccountShare Share_Record =
new
AccountShare();
Share_Record.AccountId = accountId;
Share_Record.UserOrGroupId =
'0055g000008edErAAI'
;
//user or group Id whom to share record
Share_Record.AccountAccessLevel =
'Edit'
;
Share_Record.OpportunityAccessLevel =
'Read'
;
// these only for account object
Share_Record.RowCause = Schema.AccountShare.RowCause.Manual;
// reason
insert Share_Record;
}
}
public
class
ShareCustomObjectRecord{
public
static
void
shareRecord(Id recordId){
MyCustomObject__Share Share_Record =
new
MyCustomObject__Share();
/* Object Record Id- Refer to custom share objects fields */
Share_Record.parentId = recordId;
Share_Record.UserOrGroupId =
'0059000000239wXAAQ'
;
//user or group Id whom to share record
Share_Record.AccessLevel =
'Edit'
;
/** Specify that the reason the user can edit the record is because its his custom object result
* (User_Access__c is the Apex Sharing Reason that we defined earlier.)
**/
Share_Record.RowCause = Schema.MyCustomObject__Share.RowCause.User_Access__c;
insert Share_Record;
}
}
😋😋😋😋😋😋😋😋😋
Comments
Post a Comment
Please Write your comment here.