You need to sign in to do that
Don't have an account?

What is the diff between with sharing and without sharing keyword?
Hi all,
Diff between with sharing and with out sharing keyword?what is default sharing while writing apex class?
Public class accclass{ }---this class with sharing or with out sharing?
Thanks,
vicky
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_sharing.htm
Let's see if I can summarize:
System Context: the code has full access to all records and fields in the system.
User Context: the code can only access records (and possibly fields, such as executeAnonymous calls) available to the user.
Without Sharing: the code runs in system context; all records and fields are available.
With Sharing: the code runs in user context; DML operations may fail because of sharing settings, and queries will only return values available to the user.
The default mode for most code transactions is "without sharing" (meaning, the code is running in system context). This means the user may be able to see data they could not normally see, or update records not normally accessible to them. A class without a sharing modifier will take on the sharing of the current context (meaning the code may be running in "with sharing" or "without sharing" depending on the source). A class with a modifier will cause the sharing mode to change as defined while inside that class.
For example:
Note that class F is "confused." It doesn't know (nor can know) if sharing is applied to it. It is best to avoid ambigious situtions by specifically marking a class as one or the other. However, this can be useful if, for example, you have a utility class that might be called from a trigger (and thus needs no sharing) or might be called from a Visualforce page (in which case, it may need sharing). This is a flexibility feature that allows reusable code, but should be used sparingly. In almost all cases, "with sharing" should be used if the user would be able to otherwise access data they shouldn't, and rarely should "without sharing" be used, but keep in mind that this mode has fewer constraints and therefore runs faster, so if you can avoid using sharing, you can make the application faster (but you shouldn't do so at the expense of security).
Just so we're clear, the example class you mentioned is the same as F (above). It doesn't know if it's in sharing mode, and will either be sharing-enabled or sharing-disabled depending on the caller.
All Answers
Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user. For example:
Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not enforced. For example:
For reference
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_sharing.htm
If this post is helpful please throw Kudos.If this post solves your problem kindly mark it as solution.
Thanks
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_keywords_sharing.htm
Let's see if I can summarize:
System Context: the code has full access to all records and fields in the system.
User Context: the code can only access records (and possibly fields, such as executeAnonymous calls) available to the user.
Without Sharing: the code runs in system context; all records and fields are available.
With Sharing: the code runs in user context; DML operations may fail because of sharing settings, and queries will only return values available to the user.
The default mode for most code transactions is "without sharing" (meaning, the code is running in system context). This means the user may be able to see data they could not normally see, or update records not normally accessible to them. A class without a sharing modifier will take on the sharing of the current context (meaning the code may be running in "with sharing" or "without sharing" depending on the source). A class with a modifier will cause the sharing mode to change as defined while inside that class.
For example:
Note that class F is "confused." It doesn't know (nor can know) if sharing is applied to it. It is best to avoid ambigious situtions by specifically marking a class as one or the other. However, this can be useful if, for example, you have a utility class that might be called from a trigger (and thus needs no sharing) or might be called from a Visualforce page (in which case, it may need sharing). This is a flexibility feature that allows reusable code, but should be used sparingly. In almost all cases, "with sharing" should be used if the user would be able to otherwise access data they shouldn't, and rarely should "without sharing" be used, but keep in mind that this mode has fewer constraints and therefore runs faster, so if you can avoid using sharing, you can make the application faster (but you shouldn't do so at the expense of security).
Just so we're clear, the example class you mentioned is the same as F (above). It doesn't know if it's in sharing mode, and will either be sharing-enabled or sharing-disabled depending on the caller.
Thanks
I have a scenario where I am creating a record of an object to which the current user doesnot have access to in a class which is called by a trigger.
So it goes like if the user updates the account record, a trigger is fired which calls a class and creates a record of the object B to which user has no access.
What will be the effect when i give without sharing and with sharing ?Will the user be able to insert records or not?
Eg : Trigger trigAccount on Account(Before Update){
classB clsobj=new ClassB();
clsobj.insertobject();
}
Scenario1
public without sharing classB{
public void insertobject(){
B obj=new B(Name='test');
insert obj;
}
}
Scenario 2
public withsharing classB{
public void insertobject(){
B obj=new B(Name='test');
insert obj;
}
}
Thanks,
Ankit
https://www.sfdc-lightning.com/2018/11/with-sharing-and-without-sharing-in.html