I need to pass parameter to the batch apex class.
This is apex class:
global class batchNotesInsert implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'select id,Parent.name,Parent.Type,Title,body from note';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Note> scope) {
List<ShowNotes__c> shn = new List<ShowNotes__c>();
for(Note a : scope)
{
shn.add
(
new ShowNotes__c
(
Name = a.Parent.name,
Title__c = a.Title,
Content__c = a.body,
ParentOfNotes__c = a.Parent.Type
)
);
}
insert shn;
}
global void finish(Database.BatchableContext BC) {
}
}
Controller Code:
public class callNotesbatchcls {
Public PageReference callingMethod()
{
batchNotesInsert shn = new batchNotesInsert();
database.executeBatch(shn); return null;
}
}
Thank you.
Best Answer
Pass in constructor.
batch
Yes Like @Eric and @crop1645 said.
If we are modifying
strParameter
in execute method then still it will have its original value. maintain the updated value instrParameter
we needdatabase.stateful
Note: If you are not modifying
strParameter
value in execute method then no need to usedatabase.stateful