function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
bprakashbprakash 

Batch apex code

hi,

I had something wrong with the batch apex,

global class AssetUpdateContract1 implements Database.Batchable<sObject> {
    
    String query = 'select Id, AssetId, Key__c, Status, StartDate, EndDate, CreatedDate, PricebookEntry.Product2.Type__c, PricebookEntry.Product2.Level__c,PricebookEntry.Product2.Sharing_Content_Option__c,PricebookEntry.Product2.License_Feature__r.Name from ContractLineItem';
    Public Boolean runTestMethod = false;
   
    global  AssetUpdateContract1() {
     }

    global  AssetUpdateContract1(String q) {
       query = q;
     }
    
    global database.queryLocator start(Database.BatchableContext BC) {
         
        if(runTestMethod) {
            query = query + ' LIMIT 200';
        }
        return database.getQueryLocator(query);

    } //Close start Method
  
     global void execute(Database.BatchableContext BC, list<ContractLineItem> ConList) {
     
           Set<Id> assetIdSet = new Set<Id>();
           Set<Id> assetIdSet1 = new Set<Id>();
       
           Set<Id> assetIdSet2 = new Set<Id>();
           Set<Id> assetIdSet3 = new Set<Id>();
         Map<Id, List<ContractLineItem>> assetCLIList = new Map<Id, List<ContractLineItem>>();

          Map<Id, List<ContractLineItem>> assetCLIList1 = new Map<Id, List<ContractLineItem>>();
           Map<Id, List<ContractLineItem>> assetCLIList2 = new Map<Id, List<ContractLineItem>>();

            Map<Id, List<ContractLineItem>> assetCLIList3 = new Map<Id, List<ContractLineItem>>();
             
         for(ContractLineItem Con : ConList){
               
             if(Con.AssetId != null && (Con.Status  =='Active' ||Con.Status  =='Inactive') && Con.PricebookEntry.Product2.License_Feature__c != null && Con.PricebookEntry.Product2.License_Feature__r.Name == 'FireEye Support') {

                     if(assetCLIList.get(Con.AssetId) != null) {
                        List<ContractLineItem> tempCLIList = (List<ContractLineItem>)assetCLIList.get(Con.AssetId);
                        tempCLIList.add(Con);

                                    } 
                                    else {
                        List<ContractLineItem> tempCLIList = new List<ContractLineItem>();
                        tempCLIList.add(Con);

                        assetCLIList.put(Con.AssetId, tempCLIList);
                         }                 
                   assetIdSet.add(Con.AssetId);
                     }
                     
bprakashbprakash
 if(assetIdSet.size() > 0) {
             Set<Id> accId = new Set<Id>();
             List<Asset> finalAss = new List<Asset>();
            
             List<Asset> assetList = [Select Id,AccountId, Name from Asset where Id in: assetIdSet];
             //Map<Id,Asset> astMap = new Map<Id,Asset>();
              List<Date> stList = new List<Date>();
                 List<Date> eList = new List<Date>();
             for(Asset ast : assetList) {
                
                 List<ContractLineItem> tempCLIList = assetCLIList.get(ast.Id);
                 system.debug('tempCLIList:'+tempCLIList);
                 accId.add(ast.AccountId);
                 system.debug('-----------------accId:'+ast.AccountId);
                 Date stDate= null,eDate= null;
                  String sKey= null,sType= null;
                 ContractLineItem fin1 = null,fin2 = null;
                
                 for(ContractLineItem cont1: tempCLIList){
                     if(cont1.StartDate != null && cont1.EndDate != null){
                     stList.add(cont1.StartDate);
                     eList.add(cont1.EndDate);
                     
                     if(fin1 == null){
                        fin1 = cont1;
                        ast.Support_Key__c = cont1.Key__c;
                        if(cont1.Status == 'Active'){
                        ast.Support_Type__c = cont1.PricebookEntry.Product2.Level__c;
                        }
                     
                        }else {
                           if(cont1.EndDate > fin1.EndDate){
                           
                             ast.Support_Key__c = cont1.Key__c;
                             fin1 = cont1;
                               if(cont1.Status == 'Active'){
                             ast.Support_Type__c = cont1.PricebookEntry.Product2.Level__c;
                             }
                             
                           } else{
                             if(cont1.EndDate == fin1.EndDate){
                                if(cont1.CreatedDate > fin1.CreatedDate){
                                   ast.Support_Key__c  = cont1.Key__c;
                                   //fin1 = cont1;
                                   if(cont1.Status == 'Active'){
                                       ast.Support_Type__c = cont1.PricebookEntry.Product2.Level__c;
                              
                                        }
                                        fin1 = cont1;
                                     }
                            
                          
                                    }
                                  }
                          
                          }
                        
                     
                     }
                      eList.Sort();
                     stList.Sort();
                     ast.Support_Start_Date__c = stList[0];
                 ast.Support_End_Date__c = eList[eList.Size() -1]; 
                     
                     
                      
                 //System.debug('--------------------------------finalCLI:'+finalCLI);

                /* ast.Support_Start_Date__c = stList[stList.size()-1];
                 ast.Support_End_Date__c = eList[0];*/
                 //ast.Support_Key__c = sKey;
                 //ast.Support_Type__c= sType;
                 
                        
             }
             }
             
              update assetList;
              
             
              
           }
bprakashbprakash

in those Contract Line items need to sort according to get the max end date for each asset,min start date for each asset.Please take a look and i needed really urgent help from the community as iam struggling for a week but still not getting the desired result,Please help me ou of this

Thanks

Kan

bprakashbprakash

any help is greatly appreciated,