+ Start a Discussion
Sangeetha TSangeetha T 

CRITICAL - HELP !!!!! TEST CLASS NOT COVERING

Hi All 

PFB Code snippet 

The goal of the batch job is basically finding the relationship (AOR) record from past year and apply the sales focus value to the new year AOR record if the sales focus value for past AOR is = 'Assigned" and the new year AOR record sales focus is "OPEN"

I'm getting 37% code coverage only, Please help where I have missed thanks 

 
global class BatchSalesfocusAORRollOver implements Database.Batchable<sObject> {

        public integer getCurrentFY(){ 
        
        Integer currentFY;      
        Organization orgInfo = [SELECT FiscalYearStartMonth, UsesStartDateAsFiscalYearName
                                FROM Organization WHERE id=:Userinfo.getOrganizationId()];
        Date today = system.today();
        if (today.month() >= orgInfo.FiscalYearStartMonth) {
            if (orgInfo.UsesStartDateAsFiscalYearName) 
                currentFY = today.year();             
            else currentFY = today.year() + 1; 
        }else {
            if (orgInfo.UsesStartDateAsFiscalYearName) 
                currentFY = today.year() - 1;
            else currentFY = today.year();            
        }
        System.debug('&&&&&&&&'+currentFY);                
        return currentFY;
    }      
             
    Integer FiscalYear = getCurrentFY();
    Integer FYPlusOne = FiscalYear - 1;
    // Variable defined to check if the previous fiscal year
    String PrevYearCheck = FYPlusOne +'%';

    global database.querylocator start(Database.BatchableContext BC){   
        Integer lmt = Limits.getLimitQueryRows();
        String National = System.Label.CL_National;
        String query = 'Select Org_SFID__r.Fiscal_Year_SFID__c,fAccount_Brand_ID__c,Id,CFY_Dealer_Account_SFID__r.Brand_Group_Name__c,Integration_Key__c,';     
        query += 'Org_SFID__c,Org_SFID__r.Fiscal_Year_SFID__r.Year__c,Sales_Focus_Descr__c';
        query += ' FROM Account_Org_Relationship__c';
        query += ' WHERE Sales_Focus_Descr__c =\'Assigned\' and is_Active__c = true and fFiscal_Year__c = false and Integration_Key__c LIKE PrevYearCheck and fAccount_Brand_Name__c =: National';
        
        System.debug('query ::::'+ query);
        return Database.getQueryLocator(query);        
    }
        
    global void execute(Database.BatchableContext BC, List<Account_Org_Relationship__c> AORCurrentFYList){
        String National = System.Label.CL_National;
        map<String,Account_Org_Relationship__c> FutureFYAORMap = new  map<String,Account_Org_Relationship__c>();
        System.debug('FiscalYear ::::'+ FiscalYear + 'FYPlusOne ::::'+ FYPlusOne );

        List<String> KeyList = new List<String>();
        for (Account_Org_Relationship__c scopeAOR: AORCurrentFYList) {
            Integer Key_length = String.valueOf(scopeAOR.Integration_Key__c).length();
            String Partial_Key = String.valueOf(scopeAOR.Integration_Key__c).substring(5, Key_length);
            String Complete_Key =  FYPlusOne + '|' + Partial_Key;
            KeyList.add(Complete_Key);
        }
        system.debug('KeyList    '+ KeyList);
  /*      List<Account_Org_Relationship__c> FutureFYAORList = [Select id,
                                                                    name,
                                                                    Sales_Focus_Descr__c,
                                                                    Integration_Key__c
                                                            From Account_Org_Relationship__c
                                                            Where Org_SFID__r.Fiscal_Year_SFID__r.name =: String.valueOf(FYPlusOne)];
 */
 
        for (Account_Org_Relationship__c AOR: [Select id,name,Sales_Focus_Descr__c,Integration_Key__c From Account_Org_Relationship__c
                                               Where is_Active__c = true and Sales_Focus_Descr__c = 'Open' AND fAccount_Brand_Name__c =: National and Org_SFID__r.Fiscal_Year_SFID__r.name =: String.valueOf(FYPlusOne) and Integration_Key__c in: KeyList])
        {
            If (AOR.Integration_Key__c !=null) {
                Integer Key_length = String.valueOf(AOR.Integration_Key__c).length();
                String Partial_Key = String.valueOf(AOR.Integration_Key__c).substring(5, Key_length);
                FutureFYAORMap.put(Partial_Key, AOR);
            }
        }
        system.debug('FutureFYAORMap :::::' + FutureFYAORMap);
        
        List<Account_Org_Relationship__c> AORUpdList = new List<Account_Org_Relationship__c>();
        Integer index = 0;
        for(Account_Org_Relationship__c AORold: AORCurrentFYList) {
            If (AORold.Integration_Key__c !=null) {
                Integer Key_length = String.valueOf(AORold.Integration_Key__c).length();
                String Partial_Key = String.valueOf(AORold.Integration_Key__c).substring(5, Key_length);
        
                if(FutureFYAORMap.containsKey(Partial_Key)) {
                    Account_Org_Relationship__c AORitem = new Account_Org_Relationship__c();
                    AORitem = FutureFYAORMap.get(Partial_Key);
                    AORitem.Sales_Focus_Descr__c = 'Assigned';
                    AORUpdList.add(AORitem);
                    index = index + 1;
                }
            }
        }
        system.debug('index @@@@@@' + index);
        database.update(AORUpdList);
    }

    global void finish(Database.BatchableContext BC) {

    }
}
 
@isTest
public class BatchSalesfocusAORRollOverTest{
 
    static testMethod void testApexFiveAOR()
     {
   
        //Creating Period Data Team record
      Integer FiscalYear;      
        Organization orgInfo = [SELECT FiscalYearStartMonth, UsesStartDateAsFiscalYearName
                                FROM Organization WHERE id=:Userinfo.getOrganizationId()
                               ];
        Date today = system.today();               
        if (today.month() >= orgInfo.FiscalYearStartMonth) {
            if (orgInfo.UsesStartDateAsFiscalYearName) {
                FiscalYear = today.year();
            }else{
                FiscalYear = today.year() + 1;
            }
        }else{
            if (orgInfo.UsesStartDateAsFiscalYearName) {
                FiscalYear = today.year() - 1;
            }else{
                FiscalYear = today.year();
            }
        }
  
      Integer FiscalYearplusone = FiscalYear - 1;
      
         
        Periods__c p = new Periods__c();
        p.CFY_Offset_ID__c = -1;
        p.Name=string.valueOf(FiscalYear-1);
        p.Year__c = FiscalYearplusone;   
        p.Period_Level_Descr__c ='Year'; 
        p.Period_ID__c ='20171';      
        insert p; 
      
        Periods__c p1 = new Periods__c();
        p1.CFY_Offset_ID__c = 0;
        p1.Name=string.valueOf(FiscalYear);
        p1.Year__c = FiscalYear;   
        p1.Period_Level_Descr__c ='Year'; 
        p1.Period_ID__c ='20181';    
        insert p1;    
        
        RecordType dealerRt = [SELECT Id FROM RecordType WHERE sObjectType='Account' and DeveloperName='Dealer'];
        RecordType growerRt = [SELECT Id FROM RecordType WHERE sObjectType='Account' and DeveloperName='Grower'];    
         
        List<Monsanto_Organizations__c> MORList = new List<Monsanto_Organizations__c>();
        //Creating Monsanto Organisation Data Team record
       
        Monsanto_Organizations__c MO3 = new Monsanto_Organizations__c();
        MO3.Name='IL Central' ;       
        MO3.Market_Type_Descr__c ='Seed and Traits';      
        MO3.Brand_Family_Name__c = 'National' ;       
        MO3.Org_Key_Cd__c ='2015|ABM|C101' ;       
        MO3.Org_Cd__c ='C101' ;       
        MO3.Org_Description__c ='IL Central';       
        MO3.Fiscal_Year_SFID__c = p.Id;
        MORList.add(MO3); 
        
        Monsanto_Organizations__c MO4 = new Monsanto_Organizations__c();
        MO4.Name='Lowell Butler' ;       
        MO4.Market_Type_Descr__c ='Seed and Traits';      
        MO4.Brand_Family_Name__c = 'National' ;       
        MO4.Org_Key_Cd__c ='2015|DSM|C1D' ;       
        MO4.Org_Cd__c ='C1D' ;       
        MO4.Org_Description__c ='Lowell Butler';       
        MO4.Fiscal_Year_SFID__c = p.Id;
        MO4.Manager_Org_SFID__c = MO3.id;
        MORList.add(MO4);  
         
        Monsanto_Organizations__c MO2 = new Monsanto_Organizations__c();
        MO2.Name='2015 - AG LAND FS INC ELV GREEN VLY' ;       
        MO2.Market_Type_Descr__c ='Seed and Traits';      
        MO2.Brand_Family_Name__c = 'National' ;       
        MO2.Org_Key_Cd__c ='2015|DealerNational50540711' ;       
        MO2.Org_Cd__c ='50540711' ;       
        MO2.Org_Description__c ='AG LAND FS INC ELV GREEN VLY';       
        MO2.Fiscal_Year_SFID__c = p1.Id;    
        MO2.Manager_Org_SFID__c = MO4.id;
        MORList.add(MO2);     
        
        database.insert(MORList, false); 
        
        //Creating Business Plan record
        Business_Plan__c bp = new Business_Plan__c();      
        bp.Name = '2015 Crop Plan' ;     
        bp.Integration_Key__c = 'Acc123asdhgasd' ;      
        bp.Market_Type_Descr__c ='All' ;      
        bp.BPY_SFID__c = p1.id;            
        insert bp ;  
        
        //Creating Sales Summary By Year record
        Sales_Summary_Level_By_Year__c ss0 = new Sales_Summary_Level_By_Year__c(is_Active__c = true,
        Category_Type_Descr__c = 'Brand',
        name='ss',
        Account_Type_Descr__c = 'Location',
        Product_Type_Descr__c = 'Bulk',
        Fiscal_Year_SFID__c =p1.id,
        Product_Summary_Level_Descr__c ='WEATHERMAX');      
        insert ss0;        

        //Creating District Sales Summary Record
        District_Sales_Summary_By_Year__c d = new District_Sales_Summary_By_Year__c( name = 'dst1',
        is_Active__c = true,
        Team_BP_SFID__c = bp.id, 
        Sales_Summary_Level_SFID__c = ss0.id,
        is_BP_Submitted__c = true,
   //     BP_Submitted_By_User_SFID__c = DSMMem.id ,
        BP_Submitted_Date__c = date.today() - 3 ,
        District_Org_SFID__c = MO2.id,
        BP_Approved_Date__c = date.today() - 4,
        BP_Rejected_Date__c = date.today() - 6);
        insert d;    
          
       // Create an Account    
       Account a2  = new Account();    
       a2.Name = 'TestDealer2';    
       a2.RecordTypeId = dealerRt.Id;
       a2.Brand_Group_Name__c = 'National';
       insert a2;

       // Create an Account    
       Account a0  = new Account();    
       a0.Name = 'TestDealer';    
       a0.RecordTypeId = dealerRt.Id;
       a0.Brand_Group_Name__c = 'National';
       insert a0; 
         
       Account a1  = new Account();    
       a1.Name = 'TestGrower';    
       a1.RecordTypeId = growerRt.Id;  
       a1.Brand_Group_Name__c = 'National';  
       insert a1;
   
       Account a3  = new Account();    
       a3.Name = 'TestGrower2';    
       a3.RecordTypeId = growerRt.Id;    
       a3.Brand_Group_Name__c = 'National';
       insert a3;
       
       
        //Creating Account Sales Summary By Year record
        Account_Sales_Summary_By_Year__c as0 = new Account_Sales_Summary_By_Year__c(Name = 'as',
        Account_SFID__c = a0.id, 
        District_Sales_Summary_SFID__c = d.Id,
        CFY_GNet_Sales_Qty__c = 26);
        insert as0;
        
        Account_Sales_Summary_By_Year__c as1 = new Account_Sales_Summary_By_Year__c(Name = 'as',
        Account_SFID__c = a2.id, 
        District_Sales_Summary_SFID__c = d.Id,
        CFY_GNet_Sales_Qty__c = 68);
        insert as1;    
     
        List<Account_Org_Relationship__c> AORListToInsert = new List<Account_Org_Relationship__c>();
        List<Account_Org_Relationship__c> AORListToUpdate = new List<Account_Org_Relationship__c>();
        
        Account_Org_Relationship__c aor0 = new Account_Org_Relationship__c(
        Name = 'TestAccOrgRelationship', 
        Account_SFID__c = a1.Id, 
        Org_Account_SFID__c = a0.Id,
        Org_SFID__c = MO2.Id, 
        Sales_Focus_Descr__c = 'Open', 
        CFY_Dealer_Account_SFID__c = a0.Id,
        Corn_Acct_Sls_Summ_SFID__c = as0.Id,
        Soy_Acct_Sls_Summ_SFID__c = as1.Id,
        CFY_Grower_Account_SFID__c = a1.Id,
        Integration_Key__c = String.ValueOf(FiscalYear) +'|test1',
        is_Active__c = true);  
        AORListToInsert.add(aor0);
        aor0.Sales_Focus_Descr__c = 'Assigned';
        AORListToUpdate.add(aor0); 

        
        Account_Org_Relationship__c aor1 = new Account_Org_Relationship__c(
        Name = 'TestAccOrgRelationship1', 
        Account_SFID__c = a1.Id, 
        Org_Account_SFID__c = a0.Id,
        Org_SFID__c = MO2.Id, 
        Sales_Focus_Descr__c = 'Open', 
        CFY_Dealer_Account_SFID__c = a0.Id,
        Corn_Acct_Sls_Summ_SFID__c = as0.Id,
        Soy_Acct_Sls_Summ_SFID__c = as1.Id,
        CFY_Grower_Account_SFID__c = a1.Id,
        Integration_Key__c = String.ValueOf(FiscalYear) +'|test2',
        is_Active__c = true);  
        AORListToInsert.add(aor1);
        aor1.Sales_Focus_Descr__c = 'Assigned';
        AORListToUpdate.add(aor1);         

        
        Account_Org_Relationship__c aor2 = new Account_Org_Relationship__c(
        Name = 'TestAccOrgRelationship2', 
        Account_SFID__c = a1.Id, 
        Org_Account_SFID__c = a0.Id,
        Org_SFID__c = MO2.Id, 
        Sales_Focus_Descr__c = 'Open', 
        CFY_Dealer_Account_SFID__c = a0.Id,
        Corn_Acct_Sls_Summ_SFID__c = as0.Id,
        Soy_Acct_Sls_Summ_SFID__c = as1.Id,
        CFY_Grower_Account_SFID__c = a1.Id,
        Integration_Key__c = String.ValueOf(FiscalYear) +'|test3',
        is_Active__c = true);  
        AORListToInsert.add(aor2);
        aor2.Sales_Focus_Descr__c = 'Assigned';
        AORListToUpdate.add(aor2);         

        
        Account_Org_Relationship__c aor3 = new Account_Org_Relationship__c(
        Name = 'TestAccOrgRelationship3', 
        Account_SFID__c = a1.Id, 
        Org_Account_SFID__c = a0.Id,
        Org_SFID__c = MO2.Id, 
        Sales_Focus_Descr__c = 'Open', 
        CFY_Dealer_Account_SFID__c = a0.Id,
        Corn_Acct_Sls_Summ_SFID__c = as0.Id,
        Soy_Acct_Sls_Summ_SFID__c = as1.Id,
        CFY_Grower_Account_SFID__c = a1.Id,
        Integration_Key__c = String.ValueOf(FiscalYear) +'|test4',
        is_Active__c = true);  
        AORListToInsert.add(aor3);
        aor3.Sales_Focus_Descr__c = 'Assigned';
        AORListToUpdate.add(aor3);         

        Account_Org_Relationship__c aor4 = new Account_Org_Relationship__c(
        Name = 'TestAccOrgRelationship', 
        Account_SFID__c = a1.Id, 
        Org_Account_SFID__c = a0.Id,
        Org_SFID__c = MO2.Id, 
        Sales_Focus_Descr__c = 'Open', 
        CFY_Dealer_Account_SFID__c = a0.Id,
        Corn_Acct_Sls_Summ_SFID__c = as0.Id,
        Soy_Acct_Sls_Summ_SFID__c = as1.Id,
        CFY_Grower_Account_SFID__c = a1.Id,
        Integration_Key__c = String.ValueOf(FiscalYear) +'|test5',
        is_Active__c = true);  
        AORListToInsert.add(aor4);
        aor4.Sales_Focus_Descr__c = 'Assigned';
        AORListToUpdate.add(aor4);         

              
       Account_Org_Relationship__c aor10 = new Account_Org_Relationship__c(
       Name = 'TestAccOrgRelationship', 
       Account_SFID__c = a1.Id, 
       Org_Account_SFID__c = a0.Id,
       Org_SFID__c = MO4.Id, 
       CFY_Dealer_Account_SFID__c = a0.Id,
       Corn_Acct_Sls_Summ_SFID__c = as0.Id,
       Soy_Acct_Sls_Summ_SFID__c = as1.Id,
       CFY_Grower_Account_SFID__c = a1.Id,
       Integration_Key__c = String.ValueOf(FiscalYearplusone) +'|test1',
       IsValid__c = false,
       Sales_Focus_Descr__c = 'Open',
       is_Active__c = true);  
       AORListToInsert.add(aor10);
       
       Account_Org_Relationship__c aor11 = new Account_Org_Relationship__c(
       Name = 'TestAccOrgRelationship', 
       Account_SFID__c = a1.Id, 
       Org_Account_SFID__c = a0.Id,
       Org_SFID__c = MO4.Id, 
       CFY_Dealer_Account_SFID__c = a0.Id,
       Corn_Acct_Sls_Summ_SFID__c = as0.Id,
       Soy_Acct_Sls_Summ_SFID__c = as1.Id,
       CFY_Grower_Account_SFID__c = a1.Id,
       Integration_Key__c = String.ValueOf(FiscalYearplusone) +'|test2',
       IsValid__c = false,
       Sales_Focus_Descr__c = 'Open',
       is_Active__c = true);  
       AORListToInsert.add(aor11);
       
       Account_Org_Relationship__c aor12 = new Account_Org_Relationship__c(
       Name = 'TestAccOrgRelationship', 
       Account_SFID__c = a1.Id, 
       Org_Account_SFID__c = a0.Id,
       Org_SFID__c = MO4.Id, 
       CFY_Dealer_Account_SFID__c = a0.Id,
       Corn_Acct_Sls_Summ_SFID__c = as0.Id,
       Soy_Acct_Sls_Summ_SFID__c = as1.Id,
       CFY_Grower_Account_SFID__c = a1.Id,
       Integration_Key__c = String.ValueOf(FiscalYearplusone) +'|test3',
       IsValid__c = false,
       Sales_Focus_Descr__c = 'Open',
       is_Active__c = true);  
       AORListToInsert.add(aor12);
       
       Account_Org_Relationship__c aor13 = new Account_Org_Relationship__c(
       Name = 'TestAccOrgRelationship', 
       Account_SFID__c = a1.Id, 
       Org_Account_SFID__c = a0.Id,
       Org_SFID__c = MO4.Id, 
       CFY_Dealer_Account_SFID__c = a0.Id,
       Corn_Acct_Sls_Summ_SFID__c = as0.Id,
       Soy_Acct_Sls_Summ_SFID__c = as1.Id,
       CFY_Grower_Account_SFID__c = a1.Id,
       Integration_Key__c = String.ValueOf(FiscalYearplusone) +'|test4',
       IsValid__c = false,
       Sales_Focus_Descr__c = 'Open',
       is_Active__c = true);  
       AORListToInsert.add(aor13);
       
       Account_Org_Relationship__c aor14 = new Account_Org_Relationship__c(
       Name = 'TestAccOrgRelationship', 
       Account_SFID__c = a1.Id, 
       Org_Account_SFID__c = a0.Id,
       Org_SFID__c = MO4.Id, 
       CFY_Dealer_Account_SFID__c = a0.Id,
       Corn_Acct_Sls_Summ_SFID__c = as0.Id,
       Soy_Acct_Sls_Summ_SFID__c = as1.Id,
       CFY_Grower_Account_SFID__c = a1.Id,
       Integration_Key__c = String.ValueOf(FiscalYearplusone) +'|test5',
       IsValid__c = false,
       Sales_Focus_Descr__c = 'Open',
       is_Active__c = true);  
       AORListToInsert.add(aor14);

        Test.startTest();
        if(AORListToInsert.size() >0 ){
            database.insert(AORListToInsert, false);
            database.update(AORListToUpdate, false);
        }   
        List<Account_Org_Relationship__c> aor = [Select fAccount_Brand_Name__c,Id,Integration_Key__c,fFiscal_Year__c,
                                           Org_SFID__c,Org_SFID__r.Fiscal_Year_SFID__r.Year__c,Sales_Focus_Descr__c FROM Account_Org_Relationship__c
                                           where Account_SFID__c = :a1.id];
        system.debug('aor ^^^^^^^^^^^^^'+aor);       
        BatchSalesfocusAORRollOver bc = new BatchSalesfocusAORRollOver();
        bc.getCurrentFY();
       database.executeBatch(bc);
        Test.stopTest();          
   
    }
}