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
NLTNLT 

what is difference between system mode and without sharing?

Mahesh DMahesh D
Hi Lakshman,

System mode -
System mode is nothing but running apex code by ignoring user's permissions. For example, logged in user does not have 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 user's permissions. 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 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 an 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 an 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.

Please look into the below links:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm

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

http://aluniya.blogspot.com/2014/02/salesforce-system-mode-user-mode-and.html

http://www.infallibletechie.com/2012/12/class-with-sharing-and-without-sharing.html


Please do let me know if it helps you.

Regards,
Mahesh
JyothsnaJyothsna (Salesforce Developers) 
Hi Lakshman,

Generally, Apex code executes in System contexts, means Access to all objects, fields and records, etc.
(beyond current users permissions, field level security or sharing settings).
Note: ( Exception ) Anonymous code blocks always execute with current user permissions :)
So take care while developing code that user doesn't see/process data which he is not supposed to.
E.g: declaring apex class method as webservice ( take a look, kind of data you process or return inside webservice apex method ).
Give suitable access for class ( to suitable profiles) , once accessed by Webservice call, then APEX executes the in system context.

:) But this is also possible to execute Apex Class with current user's "Sharing settings". using "With Sharing" keyword. ( consider Salesforce sharing setting while executing Apex code )

Note: "With sharing" keyword doesn't enforce the user's permissions and field-level security.

E.g.

public with sharing class MyNewClassWithSharing {
// Code here
}

Use the without sharing keywords means sharing rules for the current user not enforced. For example:

public without sharing class MyNewClassWithOutSharing {
// Code here
}
Points to be noted:
A)  If with or without sharing are not mentioned with class then the default is "Without sharing".

B)  If the class is define with NO "sharing type" and called with in other class, then it will execute with calling class's sharing setting accordingly.
    Else executes with its own settings.
   
B)  The sharing setting applies to all code contained with in the class, including initialization code, constructors, and methods.

C)  Classes inherit this setting from a parent class when one class extends or implements another.

D)  Inner classes do not inherit the sharing setting from their container class.

E)  Class declared as with sharing can call code that operates as without sharing

F)  All SOQL or SOSL queries that use PriceBook2 ignore the with sharing keyword

G)  Using with sharing keyword:
i) SOQL and SOSL queries may return fewer rows
ii) DML operation may fail due to insufficient privileges for the current user.

Please refer the below link for more details
http://aluniya.blogspot.sg/2014/02/salesforce-system-mode-user-mode-and.html

Hope this helps you!
Best Regards,
Jyothsna
pranav dinakaran 11pranav dinakaran 11
https://salesforce-fundamentals-by-pranav.blogspot.in

Share record without writing apex code
farukh sk hdfarukh sk hd