While executing Apex Batch I might need the same Custom Settings / Metadata Values

So which is the best practice

  1. Getting the data each time the execute method executes (Number of times depends on the number of records and batch size).
  2. Setting the value in Instance variable via the constructor when the instance of the batch is created for invoking the batch class & using it across multiple transactions that the batch would execute.

2 Answers 2


It depends on your design intent. If you want the settings to remain constant during the entire execution cycle, you can initialize the values once during the constructor. If you want the batch to be able to react to changes "immediately," retrieve the custom settings during the execute call. I've used both designs in various projects, and it always comes down to if you expect the additional flexibility.

As an example of the during-execute method, I had an API dispatcher bath class that needed to have the ability to enable/disable/throttle API calls in real-time. Providing the option to change it in realtime allowed the batch to run for an hour at a time without constant restarts, drastically improving performance and reliability. In many other cases, I only needed to load the settings on startup, so the values were initialized in the constructor or start method.


It won't matter much. However, if your batch is not already stateful, making it so just for the purpose of caching this data will almost certainly reduce performance. As it is, you will get very little benefit from caching the data anyway, as retrieving custom settings is very inexpensive. I recommend you simply retrieve them for each execute call.

  • It doesn't need to be stateful to cache the data; anything you have set previously would be retained.
    – sfdcfox
    May 2, 2019 at 18:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .