高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
40
41
42
43
44
45
46
47
48
public with sharing class ConsumableOrderMinorBatch implements Database.Batchable<SObject>, Database.Stateful{
 
    private List<String> ids;
    public ConsumableOrderMinorBatch(){
        
    }
 
    public ConsumableOrderMinorBatch(List<String> ids){
        this.ids = ids;
    }
 
    public Database.QueryLocator start(Database.BatchableContext BC) {
        String staSql = 'select id,sNo__c,Agencyinfo_fromSAP__c,TracingCode__c,Bar_Code__c,Consumable_order_minor__c from Consumable_order_details2__c '+
        'where Agencyinfo_fromSAP__c = \'\' and TracingCode__c !=  \'\' and Bar_Code__c != \'\' and Consumable_order_minor__c  = \'\'';
        if (ids != null && ids.size() > 0) {
            staSql += ' and id in :ids';
        }
        return Database.getQueryLocator(staSql);
    }
 
    public void execute(Database.BatchableContext BC, List<Consumable_order_details2__c> cods) {
        Set<String> sNos = new Set<String>();
        for(Consumable_order_details2__c cod :cods){
            sNos.add(cod.sNo__c);
        }
        List<Asset> ass =  [select id,SerialNumber,Backorder__r.name,Backorder__r.Agency1_Name_F__c from Asset where SerialNumber in :sNos];
        
        for (Asset a: ass) {
            for(Consumable_order_details2__c cod : cods){
                if(a.SerialNumber == cod.sNo__c){
                    if (Test.isRunningTest()) {
                        cod.TextName__c = 'TextName__Test'+a.Id;    
                    }else {
                        cod.TextName__c = a.Backorder__r.Agency1_Name_F__c;
                    }
                }
            }            
        }    
        update cods;
 
    }
 
    public void finish(Database.BatchableContext BC) {
        System.debug('finish');
    }
 
  
}