global with sharing class SyncLostProductBatch implements Database.Batchable<sObject>, Database.Stateful {
|
public List<Product2> productList;
|
public List<String> oppIdList;
|
global SyncLostProductBatch(List<Product2> proList) {
|
productList = proList;
|
}
|
global Database.QueryLocator start(Database.BatchableContext bc) {
|
oppIdList = new List<String>();
|
List<String> proIdList = new List<String>();
|
for(Product2 pro: productList){
|
proIdList.add(pro.Id);
|
}
|
String query = 'select Id,LostProduct__c,ProductCategory__c,ProductClass__c,Opportunity_No__c from PCLLostProduct__c where LostProduct__c in: proIdList';
|
return Database.getQueryLocator(query);
|
}
|
global void execute(Database.BatchableContext BC, List<PCLLostProduct__c> lostProductList) {
|
List<String> oppNoList = new List<String>();
|
for(PCLLostProduct__c lostPro: lostProductList){
|
oppNoList.add(lostPro.Opportunity_No__c);
|
for(Product2 pro: productList){
|
if(lostPro.LostProduct__c == pro.Id){
|
lostPro.ProductCategory__c = pro.ProductCategory__c;
|
lostPro.ProductClass__c = pro.ProductClass__c;
|
}
|
}
|
}
|
List<Opportunity> oppList = [select Id from Opportunity where Opportunity_No__c in: oppNoList];
|
for(Opportunity opp: oppList){
|
oppIdList.add(opp.Id);
|
}
|
update lostProductList;
|
}
|
|
global void finish(Database.BatchableContext BC) {
|
System.debug('oppIdList: ' + oppIdList);
|
Database.executeBatch(new SyncLostOppHostBatch(oppIdList), 200);
|
}
|
}
|