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
ram123ram123 

global with sharing class- permission

I just want to check, global with sharing class enforces same user level permission as public with sharing class ?

 

Thanks

Ram

 

Best Answer chosen by Admin (Salesforce Developers) 
ShamilShamil

Correct. The meaning of 'with sharing' doesn't change whether the class is global or public.

The meaning of global  modifier is:

 

"... means the method or variable can be used by any Apex code that has access to the class, not just the Apex code in the same application. This access modifier should be used for any method that needs to be referenced outside of the application, either in the Web services API or by other Apex code. If you declare a method or variable as global, you must also declare the class that contains it as global."

 

"We recommend using the global access modifier rarely, if at all. Cross-application dependencies are difficult to maintain. "


Link:

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_...

 

In other words, unless you'd like to create a managed package and would like others to be able to call methods from your package don't use 'global' modifier.

All Answers

ShamilShamil

Correct. The meaning of 'with sharing' doesn't change whether the class is global or public.

The meaning of global  modifier is:

 

"... means the method or variable can be used by any Apex code that has access to the class, not just the Apex code in the same application. This access modifier should be used for any method that needs to be referenced outside of the application, either in the Web services API or by other Apex code. If you declare a method or variable as global, you must also declare the class that contains it as global."

 

"We recommend using the global access modifier rarely, if at all. Cross-application dependencies are difficult to maintain. "


Link:

http://www.salesforce.com/us/developer/docs/apexcode/index_Left.htm#StartTopic=Content/apex_classes_...

 

In other words, unless you'd like to create a managed package and would like others to be able to call methods from your package don't use 'global' modifier.

This was selected as the best answer
ram123ram123

Thanks Shamil. 

Phil WPhil W
The link to the access modifiers documentation is currently https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_access_modifiers.htm

The link to documentation about with and without sharing is currently https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_sharing.htm
DeeptiShuklaDeeptiShukla
With Sharing
The with sharing keyword allows you to specify that the sharing rules for the current user are considered for the 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. This strategy ensures that code doesn’t fail to run because of hidden fields or objects for a user. The only exceptions to this rule are Apex code that is executed with the executeAnonymous call and Connect in Apex. executeAnonymous always executes using the full permissions of the current user. For more information on executeAnonymous, see Anonymous Blocks.
Use the with sharing keywords when declaring a class to enforce the sharing rules that apply to the current user. For example:

public with sharing class sharingClass {
 
// Code here
 
}

Without Sharing
Use the without sharing keywords when declaring a class to ensure that the sharing rules for the current user are not enforced. For example, 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
 
}