You need to sign in to do that
Don't have an account?
Shanmuga Prasath Pushpanathan
how to write a scheduler apex for the batch apex which has parameter?
When I tried to schedule batch apex, I got error saying that Contructor is not defined so i have created the scheduler apex
Batch apex :
global class batchclass implements Database.Batchable<sObject>{
Set<Id> accountIdSet = new Set<Id>();
global batchclass(Set<Id> accountIdSet){
if(accountIdSet != null){
this.accountIdSet= accountIdSet;
}
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('SELECT Id, Region__c,'+
'FROM Account '+
'Where Id IN: accountIdSet');
}
global void execute(Database.BatchableContext BC, List<SObject> scope) {
//logic
}
global void finish(Database.BatchableContext BC) {
//doing nothing
}
}
Sheduler apex :
global class Schedulerapex implements Schedulable {
Set<Id> accntSet = new Set<Id>();
//constructor
global batchclass(Set<Id> accountIdSet){
accntSet = accountIdSet;
}
global void execute(SchedulableContext sc) {
batchclass updateAccountBatch = new batchclass(accntSet);
database.executebatch(updateAccountBatch, 1000);
}
}
In this scheduler apex how to pass the accountIdSet?
Do we need to query the account in the scheduler apex for passing the parameter to batch class?
Batch apex :
global class batchclass implements Database.Batchable<sObject>{
Set<Id> accountIdSet = new Set<Id>();
global batchclass(Set<Id> accountIdSet){
if(accountIdSet != null){
this.accountIdSet= accountIdSet;
}
}
global Database.QueryLocator start(Database.BatchableContext BC){
return Database.getQueryLocator('SELECT Id, Region__c,'+
'FROM Account '+
'Where Id IN: accountIdSet');
}
global void execute(Database.BatchableContext BC, List<SObject> scope) {
//logic
}
global void finish(Database.BatchableContext BC) {
//doing nothing
}
}
Sheduler apex :
global class Schedulerapex implements Schedulable {
Set<Id> accntSet = new Set<Id>();
//constructor
global batchclass(Set<Id> accountIdSet){
accntSet = accountIdSet;
}
global void execute(SchedulableContext sc) {
batchclass updateAccountBatch = new batchclass(accntSet);
database.executebatch(updateAccountBatch, 1000);
}
}
In this scheduler apex how to pass the accountIdSet?
Do we need to query the account in the scheduler apex for passing the parameter to batch class?