+ Start a Discussion
F SmoakF Smoak 

pass collection from one batch to another

Hi,

I have written a batch class which inserts "mccp" list based on "poaSet" Now for newly created mccp from batch class I need to insert "newRuleList" which has lookup to mccp id and poa id. I have invoked 2nd batch from 1st batch finish method but when i execute the first batch, both runs successfully.For Batch 1 4 MCCP inserted, whereas for BAtch 2 instead of 4 rulelist only 1 is inserted. I am unable to debug why only one MCCP is picked up to create rule list.Please help as I am in a fix!

Finish method of batch 1:
global void finish(Database.BatchableContext BC){
if(poaSet.size()>0 && mccp.size()>0){
CMS_Batch_Insert_MC_Cycle_Plan_Rule_List rulebatch = new CMS_Batch_Insert_MC_Cycle_Plan_Rule_List(poaSet,mccp);
Database.executeBatch(rulebatch, 200);
}
}

Batch 2:

global class CMS_Batch_Insert_MC_Cycle_Plan_Rule_List implements Database.Batchable<sObject>,Database.Stateful{
Map<id,id> poaCycle = New Map<id,id>();
Map<id,id> mccpCycle = New Map<id,id>();
Map<id,id>poamccp= new Map<id,id>();
Set<id> poaSet = new Set<id>();
List<MC_Cycle_Plan_vod__c> mccp= new List<MC_Cycle_Plan_vod__c>();
public CMS_Batch_Insert_MC_Cycle_Plan_Rule_List(Set<Id> poaSet, List<MC_Cycle_Plan_vod__c> mccp)
{
this.poaSet = poaSet;
this.mccp = mccp;}
global Database.QueryLocator start(Database.BatchableContext BC)
{
return Database.getQueryLocator('SELECT id,CMS_MC_Cycle__c from CMS_Rule__c where id in: poaSet');
}
global void execute(Database.BatchableContext BC, List<sobject> scope){
List<CMS_Rule_Status__c> newruleList = new List<CMS_Rule_Status__c>();
for(CMS_Rule__c poaRecs:(List<CMS_Rule__c>)scope){
poaCycle.put(poaRecs.CMS_MC_Cycle__c,poaRecs.id);
} system.debug('poacycle>>'+poaCycle);
List<MC_Cycle_Plan_vod__c> mccpList = [Select id,cycle_vod__c from MC_Cycle_Plan_vod__c where cycle_vod__c in: poaCycle.keyset()];
for(MC_Cycle_Plan_vod__c mcRecs: mccpList){
mccpCycle.put(mcRecs.cycle_vod__c,mcRecs.id);
}system.debug('mccpCycle>>'+mccpCycle);
for(Id cycid:mccpCycle.keySet()){
if(poaCycle.containsKey(cycid)){
poamccp.put(poaCycle.get(cycid),mccpCycle.get(cycid));
}system.debug('poamccp>>'+poamccp);
}
if(poamccp.size()>0){
List<CMS_Rule_Status__c> existingRecs = [Select id,MC_Cycle_Plan__c,Rule_Name__c from CMS_Rule_Status__c where Rule_Name__c in : poamccp.keyset()];
if(existingRecs.size()>0){
for(CMS_Rule_Status__c existingrule:existingRecs){
if(poamccp.containsKey(existingrule.Rule_Name__c)){
poamccp.remove(existingrule.Rule_Name__c);
}
}}}
if(poamccp.size()>0){
for(id j1:poamccp.keyset()){
CMS_Rule_Status__c rulelist = new CMS_Rule_Status__c();
rulelist.MC_Cycle_Plan__c=poamccp.get(j1);
rulelist.Rule_Name__c=j1;
rulelist.Status__c='In Progress';
newRuleList.add(rulelist);
}}
if(newRuleList.size()>0){
insert newRuleList;
}
}
global void finish(Database.BatchableContext BC){
}
}