Activity Forums Salesforce® Discussions Why Salesforce batch apex class and methods using global keyword?

  • Saurabh

    Member
    February 12, 2018 at 8:26 am

    Hello Ankit

    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 globalaccess 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 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.

    Hope it helps

    Thanks

     

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos