# How do you pass runtime parameters into a Batch Apex class prior to execution?

**Topic:** Apex · **Difficulty:** Medium · **Tags:** Apex, Asynchronous Apex, Batch Apex

Read it on the site: https://www.forcechallenges.com/interview-questions/how-do-you-pass-runtime-parameters-into-a-batch-apex-class-prior-to-execution

## Answer

**Basics:** `Database.executeBatch` takes only the batch instance and the scope size, so everything the job needs (filters, record Ids, flags, a user to notify) has to be set on the object before you submit it.

**How it works:**
1. Define a constructor with the parameters.
2. Save them in private instance fields.
3. Use them in `start()` to build the query (via bind variables) and later in `execute()`.
4. Instantiate and launch: `Database.executeBatch(new MyBatch(args), 200)`.

**Advanced / deep dive:** The batch instance is serialized between `start`, each `execute`, and `finish`, so fields must be serializable; non-serializable members need the `transient` keyword and rebuilding. Instance fields set in the constructor are retained for every chunk without needing `Database.Stateful`, because they are part of the serialized instance; `Stateful` is only needed for values that you change inside `execute()` and want to see in later chunks. Large parameters (for example thousands of Ids) inflate every serialization, so pass a filter or a staging record Id instead. From a scheduled class, build the job inside `Schedulable.execute()` and read parameters from Custom Metadata so admins can change them. Use bind variables rather than string concatenation to avoid SOQL injection.

**Code / config example:**
```apex
public class RegionRecalcBatch implements Database.Batchable<SObject> {
    private final String region;
    private final Date since;

    public RegionRecalcBatch(String region, Date since) {
        this.region = region;
        this.since = since;
    }
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocatorWithBinds(
            'SELECT Id FROM Account WHERE Region__c = :region AND LastModifiedDate >= :since',
            new Map<String,Object>{ 'region' => region, 'since' => since },
            AccessLevel.USER_MODE);
    }
    public void execute(Database.BatchableContext bc, List<Account> scope) { /* ... */ }
    public void finish(Database.BatchableContext bc) {}
}
// Database.executeBatch(new RegionRecalcBatch('EMEA', Date.today().addDays(-7)), 200);
```

**Hidden gotchas & must-remember points:**
- Batch classes need a public or global no-arg-compatible usage path only if scheduled from UI as Schedulable; the batch itself can have any constructor.
- Constructor values persist across chunks; mutated values do not unless `Database.Stateful`.
- Do not pass sObject lists with thousands of records; pass Ids or criteria.
- Static variables are not serialized and reset each chunk.
- Never concatenate user input into the query string.

**Best practice:** Keep parameters small and immutable (`final`), and use bind variables for filters.

---
Source: ForceChallenges · last updated 2026-11-02
