liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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);
    }
}