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
Yuvraj ProgrammerYuvraj Programmer 

Why global access modifier needs to be defined in Batch apex class?

Hi All,

I am new to batch class. Just wondering why we are using global access modifier instead of public. By referring to many developer forums, global is used to access the batch class outside the application whereas public used inside the application.. I see many blogs are giving example with global class eventhough Salesforce blogs too. 

Below code I used to coding practice for batch class:
 
public class AccoutBatchProcess implements Database.Batchable<Sobject>{
    
    public database.QueryLocator start(Database.BatchableContext ctx){
        return database.getQueryLocator('SELECT ID FROM Account');
    }
    
    public void execute(Database.BatchableContext ctx, List<Sobject> sList){
        
    }
    public void finish(Database.BatchableContext ctx){
        
    }
    
}
Is that mandatory that need to be replace global with public? Is that best practice to define the batch class as global?
 
Best Answer chosen by Yuvraj Programmer
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Yuvraj,
  • A Database.Batchable class has to be a top-level class (not an inner class) but can now be public.

When Database.Batchable was first available, implementors had no choice but to use the global access modifier. For people creating managed packages, this had the very unfortunate consequence of making any Database.Batchable part of the managed package's API whether you wanted it to be or not. global makes a class usable by code outside of a managed package; public makes a class usable by any class within the managed package or within a non-packaged set of source code.

This error has been corrected so that you now have the choice of making the Database.Batchable public or global and the public is the right choice most of the time. Unfortunately, I don't think the documentation has been updated.

private not global or public is the right access modifier for fields most of the time. Sometimes final is helpful in communicating intent to the reader and/or enforcing its constraint. But a more important point about fields in a Database.Batachable implementation class is that state is not maintained in fields between invocations of the methods unless the implementation class is also marked as Database.Stateful.

Please refer the below link for reference.
Hope it helps.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar

All Answers

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Yuvraj,
  • A Database.Batchable class has to be a top-level class (not an inner class) but can now be public.

When Database.Batchable was first available, implementors had no choice but to use the global access modifier. For people creating managed packages, this had the very unfortunate consequence of making any Database.Batchable part of the managed package's API whether you wanted it to be or not. global makes a class usable by code outside of a managed package; public makes a class usable by any class within the managed package or within a non-packaged set of source code.

This error has been corrected so that you now have the choice of making the Database.Batchable public or global and the public is the right choice most of the time. Unfortunately, I don't think the documentation has been updated.

private not global or public is the right access modifier for fields most of the time. Sometimes final is helpful in communicating intent to the reader and/or enforcing its constraint. But a more important point about fields in a Database.Batachable implementation class is that state is not maintained in fields between invocations of the methods unless the implementation class is also marked as Database.Stateful.

Please refer the below link for reference.
Hope it helps.

Please mark it as best answer if the information is informative.

Thanks
Rahul Kumar
This was selected as the best answer
Yuvraj ProgrammerYuvraj Programmer
@Rahul

Thanks Rahul for sharing those links. It helped me really a lot.