function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rajesh T 19Rajesh T 19 

When using a class with sharing keyword what are the functions will be enforced ?

I read some blogs states only sharing rules for the current user are considered for the class it seems sharing rules on the record level visibility. But some blogs says Field level security of the current user also enforced.
 Am confused here so can anyone tell me what are all the functions are considered when using Sharing keyword.

 
Yogendra JangidYogendra Jangid
Hi Rajesh
Use the with sharing or without sharing keywords on a class to specify whether sharing rules must be enforced. Use the inherited sharing keyword on an Apex class to run the class in the sharing mode of the class that called it.
You can go through below documentation for detailed explaination.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm

Hope this answers your question, if so please can you mark this as best answer. Thanks.
mukesh guptamukesh gupta
Hi Rajesh,

With sharing Keyword 

 
This keyword enforces sharing rules that apply to the current user. If absent, code is run under default system context.

Example
 
public with sharing class MysharingClass {
    // Code will enforce current user's
    // sharing rules
}


Without sharing keyword

Ensures that the sharing rules of the current user are not enforced.

Example
 
public without sharing class noSharing {
    // Code won't enforce current user's
    // sharing rules
}


if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh