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
39
global class TrackConsumableSalesDailyBatch implements Database.Batchable<sObject> {
    public String query;
    public String tcsfid;
    global TrackConsumableSalesDailyBatch() {
        this.query = query;
    }
     global TrackConsumableSalesDailyBatch(String tcsfid) {
        this.query = query;
        this.tcsfid = tcsfid;
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        query = 'SELECT id,OwnerId,Hospital__r.ET_owner__c,Hospital__r.ET_owner__r.IsActive FROM TrackConsumableSalesForecast__c WHERE TCSOwner__c = false';
        if (tcsfid !=null) {
           query += ' and id = :tcsfid';
        }
        return Database.getQueryLocator(query);
    }
 
    global void execute(Database.BatchableContext BC, list<TrackConsumableSalesForecast__c> scope) {
        List<TrackConsumableSalesForecast__c> UpdateList = new List<TrackConsumableSalesForecast__c>();
        for(TrackConsumableSalesForecast__c tcsf :scope){
            TrackConsumableSalesForecast__c upDatetcsf = new TrackConsumableSalesForecast__c();
            upDatetcsf.id = tcsf.id;
            if (tcsf.Hospital__r.ET_owner__r.IsActive == true) {
                upDatetcsf.OwnerId = tcsf.Hospital__r.ET_owner__c;
                UpdateList.add(upDatetcsf);
            }
        }
        if(!UpdateList.isEmpty()){
            update UpdateList;
        }  
    }
 
    global void finish(Database.BatchableContext BC) {
 
    }    
    
}