APEX ACCESS MODIFIERS - With Sharing • Without Sharing
Access Modifiers
Private:
- If you declare a class as a private, it is only known to the block in which it is declared.
- By default all the inner classes are private.
Public:
- If you declare a class as a public, this apex class is visible throughout your application and you can access the application anywhere.
Global:
- If you declare a class as a global, this apex class is visible to all the apex application in the application or outside the application.
- Note: If a method or a class (inner) is declared as global then the top level class also must be declared as global.
With Sharing:
- If you declare a class as a With Sharing, sharing rules given to the current user will be taken into consideration and the user can access and perform the operations based on the permissions given to him on objects and fields. (field level security, sharing rules)
Without Sharing:
- If you declare a class as a Without Sharing then this apex class runs in system mode which means apex code has access to all the objects and fields irrespective of current users sharing rules, fields level security, object permissions.
- Note: If the class is not declared as With Sharing or Without Sharing then the class is by default taken as Without Sharing.
- Both inner class and outer classes can be declared as With Sharing.
- If inner class is declared as With Sharing and top-level class is declared as without sharing, then the entire context will run in With Sharing context.
- Outer class is declared as With Sharing and inner class is declared as Without Sharing then inner class runs in Without Sharing context only. (Inner classes don't take the sharing properties from outer class).
Virtual:
- If a class is declared with keyword Virtual then this class can be extended (Inherited) or this class method can be overridden by using a class called overridden.
Abstract:
- This class contains Abstract methods, which will provide common method implementation to all subclasses.
Example 1:
=============================================
public class outerclass
{
//code
class innerclass
{
//Innerclass code
}
}
=============================================
Example 2:
=============================================
public with sharing class sharing class
{
//code
}
=============================================
Example 3:
=============================================
public without sharing class noSharing
{
//code
}
=============================================
Example 4:
=============================================
public with sharing class outer
{
//outer class code
without sharing class inner
{
//Inner class code
}
}
=============================================
In the above code outer class runs with current users sharing rules. But inner class runs with system context.
Example 3:
=============================================
public without sharing class noSharing
{
//code
}
=============================================
Example 4:
=============================================
public with sharing class outer
{
//outer class code
without sharing class inner
{
//Inner class code
}
}
=============================================
In the above code outer class runs with current users sharing rules. But inner class runs with system context.
Example 5:
=============================================
public without sharing class outer
{
//Outer class code
with sharing class inner
{
//Inner class code
}
}
=============================================
In this way, both the inner and outer classes run with current users permissions.
If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you
Happy learning ☁️⚡️ (Learn. Help. Share.)
Happy learning ☁️⚡️ (Learn. Help. Share.)
APEX ACCESS MODIFIERS - With Sharing • Without Sharing
Reviewed by
on
Rating:
What about the inherited sharing? I would say that is the default used keyword when it is not specified.
ReplyDeleteHi jdkgabri,
DeleteYou are absolutely correct, Inherited sharing is the default used keyword so that system can run in sharing mode 😊
Thanks, Happy Learning #salesforceKid
"If you declare a class as a Without Sharing then this apex class runs in system mode which means apex code has access to all the objects and fields irrespective of current users sharing rules, fields level security, object permissions." This statement is partially wrong. Without sharing keyword has no effect on "access to all the objects and fields".. Sharing keyword on controls record access..
ReplyDeleteNice explanation
ReplyDeleteHowever, Example 5 of the article is wrong, i have tried by running the code itself.
if outer is without sharing
and inner is with sharing
then outer will run in without sharing mode and will return all the records.
and inner will run in with sharing mode and will return the records which the user has access to