Activity › Forums › Salesforce® Discussions › How can we make batch class iterable in Salesforce?
-
How can we make batch class iterable in Salesforce?
Posted by sushant on December 16, 2016 at 1:36 PMHi All,
How can we make batch class iterable?
please give suggestions
Thanks
Vikas Kumar replied 9 years, 4 months ago 2 Members · 1 Reply -
1 Reply
-
Hi sushant,
We can make batch iterable by calling batch again inside finish method for more detail go through below code
global class iterablebatch implements Database.Batchable<sobject>{
global String Query;
global List<id>allObjIds ;
global string idInitials;
global iterablebatch(List<id>allObjectIds,String var ){
allObjIds=allObjectIds;
idInitials =var;
}
global Database.QueryLocator Start(Database.BatchableContext BC){
if (idInitials==’001′)
query=’SELECT Id,Name FROM Account WHERE Id in:allObjIds’;
if(idInitials==’003′)
query=’SELECT Id,Name FROM Contact WHERE Id in:allObjIds’;
if(idInitials==’006′)
query=’SELECT Id,Name FROM Opportunity WHERE Id in:allObjIds’;
return Database.getQueryLocator(query);
}
global void execute (Database.BatchableContext BC,List<Sobject>scope){
Schema.SObjectType sObjectType =scope.getSObjectType();
String listType = ‘List<‘ + sObjectType + ‘>’;
List<SObject> castRecords = (List<SObject>)Type.forName(listType).newInstance();
castRecords.addAll(scope);
if (idInitials==’001′){for(sobject obj:castRecords){
Account acc= (account) obj;acc.Name=’Algoworks1′;
}}
if (idInitials==’003′){for(sobject obj:castRecords){
contact con=(contact)obj;
con.LastName=’Algoworks1′;}
}
if (idInitials==’006′){for(sobject obj:castRecords){
opportunity opp=(opportunity)obj;
opp.Name=’Algoworks1′; }
}
update scope;
}
global void finish (Database.BatchableContext BC){
iterablebatch objc = new iterablebatch(allObjIds,’003′);
iterablebatch objo = new iterablebatch(allObjIds,’006′);}
}Hope it may helps
Log In to reply.