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');
    }

  
}