What is with and without sharing in Apex?

+2 votes
asked Nov 27, 2015 in Developer by bukolasf (400 points)

1 Answer

0 votes
answered Dec 4, 2015 by darsh (3,020 points)

The with sharing keyword allows you to specify that the sharing rules for the current user be taken into account for a class. You have to explicitly set this keyword for the class because Apex code runs in system context. In system context, Apex code has access to all objects and fields— object permissions, field-level security, sharing rules aren’t applied for the current user.

public with sharing class sharingClass {

// Code here

}

The without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not enforced. For example, you may want to explicitly turn off sharing rule enforcement when a class acquires sharing rules when it is called from another class that is declared using with sharing.

public without sharing class noSharing {

// Code here

}
Welcome to I Can Crack, where you can ask questions and receive answers from other members of the community.

68 questions

47 answers

3 comments

5,318 users

...