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
Hitesh chaudhariHitesh chaudhari 

what is apex class with shearing and without shearing

  1. what is apex class with shearing and without shearing
  2. what if we called apex with shearing class from another class who is without shearing and vice versa?
  3. if apex  without shearing dont consider looged in user's field/object setting in consideration then what is use of System mode? 
AshishkAshishk
Please visit below links, will give you complete idea:-

https://developer.salesforce.com/forums/?id=906F000000091XwIAI

https://salesforce.stackexchange.com/questions/16121/sfdc-understanding-with-sharing-without-sharing-unspecified-sharing-classes

Hope this helps.
Thanks
NagendraNagendra (Salesforce Developers) 
Hi Nick,

System mode -

System mode is nothing but running apex code by ignoring user's permissions. For example, logged in user does not have to create permission but he/she is able to create a record.
In system mode, Apex code has access to all objects and fields— object permissions, field-level security, sharing rules aren't applied for the current user. This is to ensure that code won’t fail to run because of hidden fields or objects for a user.
In Salesforce, all apex code run in system mode. It ignores the user's permissions. The only exception is anonymous blocks like developer console and standard controllers. Even runAs() method doesn't enforce user permissions or field-level permissions, it only enforces record sharing.

User mode - 

User mode is nothing but running apex code by respecting user's permissions and sharing of records. For example, logged in user does not have to create permission and so he/she is not able to create a record.
In Salesforce, only standard controllers and anonymous blocks like developer console run in user mode.

Without Sharing keyword - 

The 'without sharing' keyword is to ensure that the sharing rules (not permissions) for the current user are not enforced.
Example - Let's consider that the OWD for Account is private, no account records are owned by or shared with a user 'u' and a "without sharing" class called MyClass is fetching account records in a list. Now if class 'MyClass' is run by user 'u' then account records will be fetched. Remember that whether the user 'u' is having full CRUD or having no CRUD, records will be fetched.

With Sharing keyword - 

The with sharing keyword allows you to specify that the sharing rules (and not permissions) 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 mode.

Example - Let's consider that the OWD for Account is private, no account records are owned by or shared with a user 'u' and a "with sharing" class called MyClass is fetching account records in a list. Now if class 'MyClass' is run by user 'u' then no records will be fetched. Remember that whether the user 'u' is having full CRUD or having no CRUD record will not be fetched.

Q)if apex without sharing don't consider logged in user's field/object setting in consideration then what is the use of System mode? 
A)There might be some scenarios where you need to execute some operation on the background and foreground irrespective of the profile where we need to run than in the system mode.

Please look into the below links:
Please do let me know if it helps you.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra
Ajay K DubediAjay K Dubedi
Hi Nick,

Sharing or without sharing are keywords on a class to specify whether sharing rules must be enforced.
With Sharing-
The with sharing keyword allows you to specify that the sharing rules for the current user are taken into account for a class. You have to explicitly set this keyword for the class because Apex code runs in system context. In the 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. This strategy ensures that code doesn’t fail to run because of hidden fields or objects for a user. Now once the class is "with sharing" the object permissions, field-level security, sharing rules are applied for the current user and fields which should not be visible/accessible and not visible or accessible.
Without Sharing-
Use them without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not enforced.
Important -

1. If a method is defined in a class declared with 'with sharing' is called by a class declared with 'without sharing', the method will execute with sharing rules enforced.
2. The class doesn’t enforce sharing rules except if it acquires sharing rules from another class. Ex. Class A (with sharing) calls a method from Class B(Without sharing) then complete context is 'with sharing'
3. Inner classes do not inherit the sharing setting from their container class.

You can explicitly turn off sharing rule enforcement when a class is called from another class that is declared using with sharing.

public without sharing class noSharing {

// Code here

}

Note:
System mode is nothing but running apex code by ignoring user's permissions.
For example, logged in user does not have to create permission but he/she is able to create a record.
The 'without sharing' keyword is to ensure that the sharing rules (not permissions) for the current user are not enforced.
Hope you find this explanation helpful.Mark it as best answer if you find it helpful.

Thanks.
Ajay Dubedi