Passing Parameter to batch apex in Salesforce

Sample Code:

global class SampleBatchClassWithParams implements Database.Batchable<sObject>, Database.Stateful {

    private String strParameter;

    global SampleBatchClassWithParams(String strParam, Set < Id > setIds) {
        strParameter = strParam;
    }

    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SOQL';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List< sObject > scope) {
    }   

    global void finish(Database.BatchableContext BC) {
    }

}

Executing the Batch:

SampleBatchClassWithParams obj = new SampleBatchClassWithParams();
Database.executeBatch('test parameter', new Set < Id > {'001d000001W34vD'});

No comments:

Post a Comment