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
shakila Gshakila G 

Trigger code coverage 0% in Production, 82 % in Sandbox

Hi All,

I wrote a trigger in Opportunity to consolidated the order value of each FE and Tele Callers. for the same, I wrote a test class.
While running the Test class am getting coverage 82% in the sandbox. When I go for Validation in Production am getting 0% code Coverage Error.

Kindly help me to overcome this issue.

My Trigger:

trigger Cumulative_Calcuation_opp on Opportunity (Before Update) {
List<Target__C> Tglist= New List<Target__C>();
List<Target__C> Tglist1= New List<Target__C>();
Map<String,ID> Mapfield = New map<String,ID>();
Map<String,ID> MapTele = New map<String,ID>();
List<Incentive__c >lstInc =New list<Incentive__c >();
List<Incentive__c >lstInc1 =New list<Incentive__c >();
List <Cumulative__c> Cumlist= New list <Cumulative__c>();
List <Cumulative__c> Cumlist1= New list <Cumulative__c>();
list<Cumulative__c > lstcum = new list<Cumulative__c >(); 
list<Opportunity > opplist = new list<Opportunity >(); 
Set<String> setAllNames = new Set<String>();  
    
       For(Opportunity opp:Trigger.New){               
       Mapfield .put(opp.Field_Executive__c,opp.ID);MapTele.put(opp.Hidden_Owner_Name__c,opp.ID);} 
       setAllNames.addAll(Mapfield.keyset()); setAllNames.addAll(MapTele.keyset());             
for(target__C objTG : [SELECT ID, name FROM target__C WHERE Name in: setAllNames AND Active__c = True]) {
                if(Mapfield.containskey(objTG.Name)){
                    TGlist1.add(objTG);}
                if(MapTele.containskey(objTG.Name)){
                    TGlist.add(objTG);}
            }
            for(Opportunity opp: trigger.new ) { 
                    if(TGlist1.size()>0){if(opp.Field_Executive__c==TGlist1[0].NAme){
                                   opp.Target__C= TGlist1[0].ID; } }
                    IF(TGlist.size()>0){if(opp.Hidden_Owner_Name__c==TGlist[0].NAme){   
                                   opp.Telecaller__c=TGlist[0].ID;}}         
            If(opp.Telecaller__c!=Null && opp.Cumulative_Updated__c==False && opp.Work_Order_Created_Date__c!=Null ){
              Cumlist=[select ID,Target__c,Type1_Customer_Order_value_MPD__c,Type1_Customer_Order_Value_PD__c,Type2_Customer_Order_value_MPD__c,Type2_Customer_Order_value_PD__c,Type3_Customer_Order_value_MPD__c,Type3_Customer_Order_value_PD__c,Type4_Customer_Order_value_MPD__c,Type4_Customer_Order_value_PD__c,From_Date__c,To_Date__c from Cumulative__c where Target__c=:opp.Telecaller__c and  From_Date__c<=:opp.Work_Order_Created_Date__c and To_Date__c>=:opp.Work_Order_Created_Date__c];
                   if(Cumlist.size()>0){For(Cumulative__c Cum:Cumlist){lstInc = [select ID from Incentive__c where ID =:Opp.IncentiveSlab__c and Opp_Discount_To__c>=:opp.Discount__c]; 
                        If(lstInc.size()>0 ){If(Opp.Customer_Slab_Type__c=='Type 4'){           
                                Cum.Type4_Customer_Order_value_PD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 2'){Cum.Type2_Customer_Order_value_PD__c+= opp.Individual_Order_value__c; opp.Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 3'){Cum.Type3_Customer_Order_value_PD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 1'){Cum.Type1_Customer_Order_Value_PD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                             }} IF(lstInc.size()<=0 ){If(Opp.Customer_Slab_Type__c=='Type 4'){Cum.Type4_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                            }Else IF(Opp.Customer_Slab_Type__c=='Type 2'){Cum.Type2_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                            }Else IF(Opp.Customer_Slab_Type__c=='Type 3'){Cum.Type3_Customer_Order_value_MPD__c += opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                            } Else IF(Opp.Customer_Slab_Type__c=='Type 1'){Cum.Type1_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c;opp.Cumulative_Updated__c=True;
                            } }lstcum .add(Cum);opplist.add(opp);} } } 
         If(opp.Target__C!=Null && opp.FE_Cumulative_Updated__c==False && opp.Work_Order_Created_Date__c!=Null ){               
              Cumlist1=[select ID,Target__c,Type1_Customer_Order_value_MPD__c,Type1_Customer_Order_Value_PD__c,Type2_Customer_Order_value_MPD__c,Type2_Customer_Order_value_PD__c,Type3_Customer_Order_value_MPD__c,Type3_Customer_Order_value_PD__c ,Type4_Customer_Order_value_MPD__c,Type4_Customer_Order_value_PD__c,From_Date__c,To_Date__c from Cumulative__c where Target__c=:opp.Target__C and From_Date__c<=:opp.Work_Order_Created_Date__c and To_Date__c>=:opp.Work_Order_Created_Date__c];
               if(Cumlist1.size()>0){For(Cumulative__c Cum:Cumlist1){ 
                       lstInc1 = [select ID from Incentive__c where ID =:Opp.IncentiveSlab__c and Opp_Discount_To__c>=:opp.Discount__c]; 
                        If(lstInc1.size()>0 ){If(Opp.Customer_Slab_Type__c=='Type 4'){ Cum.Type4_Customer_Order_value_PD__c+= opp.Individual_Order_value__c;opp.FE_Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 2'){Cum.Type2_Customer_Order_value_PD__c+= opp.Individual_Order_value__c;opp.FE_Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 3'){Cum.Type3_Customer_Order_value_PD__c += opp.Individual_Order_value__c; opp.FE_Cumulative_Updated__c=True;
                             }Else IF(Opp.Customer_Slab_Type__c=='Type 1'){Cum.Type1_Customer_Order_Value_PD__c+= opp.Individual_Order_value__c;   opp.FE_Cumulative_Updated__c=True;
                             }} IF(lstInc1.size()<=0 ){ If(Opp.Customer_Slab_Type__c=='Type 4'){Cum.Type4_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c; opp.FE_Cumulative_Updated__c=True;
                        }Else IF(Opp.Customer_Slab_Type__c=='Type 2'){ Cum.Type2_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c; opp.FE_Cumulative_Updated__c=True;
                        }Else IF(Opp.Customer_Slab_Type__c=='Type 3'){ Cum.Type3_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c; opp.FE_Cumulative_Updated__c=True;
                        } Else IF(Opp.Customer_Slab_Type__c=='Type 1'){Cum.Type1_Customer_Order_value_MPD__c+= opp.Individual_Order_value__c;opp.FE_Cumulative_Updated__c=True;}        
                        }lstcum .add(Cum); opplist.add(opp);
                      } } } 
                      update lstcum ; }}

My Test Class: 

@isTest(seealldata=true)
private class OI_Incentivetest{
    static testMethod void OI_Incentivetest(){
    
        GetInitialData Getdata = new GetInitialData();
        list<Account> acc = Getdata.getAccount();
        list<Product2> lstprod = Getdata.getProd();
        list<Opportunity> Opp = Getdata.getOpp();
        list<Pricebook2> lstPB = Getdata.getpricebook();
        list<Pricebookentry> lststdPbe = Getdata.getstdpbe();
        list<Pricebookentry> lstPbentry = new list<Pricebookentry>();
        list<attachment> lstattachment =  new list<attachment>();
        list<Payment__C> PMT = New List<payment__C>();
        insert lstprod; 
        Integer NofoDays = 0;
        list<Opportunity> lstOpp = new list<Opportunity>();
        list<IncentiveLineItem__c> lstIli= new list<IncentiveLineItem__c>();
         
        
        
        Incentive__c incindoor= new Incentive__c(Name='Indoor test', Active__c = true, Product_Type__c = 'Indoor', 
                Start_Date__c = date.newinstance(2014,12,25), Area__c = 'All India');
        insert incindoor;
        
         IncentiveLineItem__c inclitem = new IncentiveLineItem__c(Incentive__c = incindoor.id,Other_Mode__c=0.80 ,Customer_Mode__c='Type 3',Neft_Cash__c=0.75 , Discount_From__c=0,Discount_To__c=10,Field_Staff__c=1,Telesales__c=1,Combined__c=2);
        IncentiveLineItem__c inclitem1 = new IncentiveLineItem__c(Incentive__c = incindoor.id,Other_Mode__c=0.80 ,Customer_Mode__c='Type 4',Neft_Cash__c=0.75 , Discount_From__c=10,Discount_To__c=20,Field_Staff__c=0.5,Telesales__c=.05,Combined__c=1);
        insert inclitem;
        insert inclitem1;
        
        Incentive__c incPE= new Incentive__c(Name='PE test', Active__c = true, Product_Type__c = 'Play Equipment', Start_Date__c = date.newinstance(2014,12,25), Area__c = 'South');
        insert incPE;
        
       IncentiveLineItem__c incpeitem = new IncentiveLineItem__c(Incentive__c = incPE.id,Customer_Mode__c='Type 4' ,Other_Mode__c=0.80 ,Neft_Cash__c=0.75 ,Discount_From__c=0,Discount_To__c=10,Field_Staff__c=1,Telesales__c=1,Combined__c=2);
        IncentiveLineItem__c incpeitem1 = new IncentiveLineItem__c(Incentive__c = incPE.id,Customer_Mode__c='Type 3',Other_Mode__c=0.80 ,Neft_Cash__c=0.75 , Discount_From__c=10,Discount_To__c=20,Field_Staff__c=0.5,Telesales__c=.05,Combined__c=1);
        insert incpeitem;
        insert incpeitem1; 

        Integer indexvalue = 0;
        if(lstprod.size()>0){
            for(pricebookentry irow :lststdPbe){
                irow.product2id = lstprod[indexvalue].id;
                indexvalue++;
            }
        }
        
        insert lststdPbe;
        insert lstPB; 

        if(lstprod.size()>0)  {
            for(pricebook2 irow :lstPB) {
                for(Product2 ipro : lstprod ){
                    Pricebookentry pbe = new Pricebookentry();
                    pbe.product2id = ipro.id;
                    pbe.pricebook2id = irow.id;
                    pbe.unitprice = 3000;
                    pbe.isActive = true;
                    lstPbentry.add(pbe);    
                }
            }
        }
        
        insert lstPbentry;
        insert acc[0];
      
        Opp[0].accountId = acc[0].id;
        insert Opp[0]; 
        
        Contact con= New Contact(FirstName='Shakila',LastName='Test',AccountID=acc[0].id);
    insert con;
        
        //Test.startTest();
        
         lstPB[0].Incentive_Slab__c=incPE.ID;
         update lstPB[0];
        
       
        
        Opp[0].Pricebook2Id = lstPB[0].id;
        opp[0].IncentiveSlab__c= lstPB[0].Incentive_Slab__c;
        opp[0].Field_Executive__c='Raman.A';
        update Opp[0];
        
        
        
        OpportunityLineItem OL = new OpportunityLineItem(opportunityid=Opp[0].id, Quantity=1, unitprice=8000,  
        pricebookentryid=lstPbentry[0].id);
        insert OL;  
        
        Quote Q = new Quote();
        Q.Name = 'QuoteTest';
        Q.OpportunityId = Opp[0].id; 
        Q.Quote_Count__c = 77;
        Q.Approved__c = true;
        Q.Quote_Number__c ='00000710';
        Q.Quotation_Date__c = date.newinstance(2018,7,12);
        Q.Pricebook2Id = lstPB[0].id;
         Q.Tax_Classification__c='IGST';
        insert Q;
        
        Quotelineitem QL = new Quotelineitem(quoteId=Q.id, Quantity =1, Unitprice=8000,pricebookentryid=lstPbentry[0].id);
        insert QL; 
        
         
        Q.Approved__c=True;
        Update Q;
        
        Opp[0].SyncedQuoteId = Q.id;
        update Opp[0];
       
        Opp[0].Stagename = 'Closed Won';
        update Opp[0];
        
        Project__c Proj = new Project__c(name='TestProj', Opportunity__c = Opp[0].id,Contact__c=con.ID, Quote__c = Q.id, Project_Type__c = 'New PE', Date_of_Installation__c = date.newinstance(2018,6,22),
        Nature_of_Case__c ='No Adv, Dspch & Erection Pending',Customer_Name__c=acc[0].id,Special_Instructions__c ='Test for Work order Pdf to be generated');    
        
        insert Proj;
        
        Payment__c pay0 = new Payment__c( Amount__c = 8400, Date__c = date.newinstance(2018,6,22), Mode_of_Payment__c = 'Cash',Opportunity__c = Opp[0].id,
        Project__C=proj.ID, Payment_Type__c ='Advance');
        insert pay0;
      
        Payment__c pay = new Payment__c( Amount__c = 8400, Date__c = date.newinstance(2018,6,22), Mode_of_Payment__c = 'Cash',Opportunity__c = Opp[0].id,
        Project__C=proj.ID, Payment_Type__c ='Full Pay');
        insert pay;
        
        opp[0].IncentiveSlab__c= lstPB[0].Incentive_Slab__c;
       
        update Opp[0];
        
        NofoDays= Proj.Date_of_Installation__c .daysBetween(pay.Date__c);
         If(NofoDays<=0)
          NofoDays=0;
              
           OPP[0].No_of_Days__c=NofoDays;
           update Opp[0];
         
         If(opp[0].IncentiveSlab__c!=Null  && Opp[0].Customer_Slab_Type__c!='Type 4')
            
            {            
              
          lstIli = [select id,Other_Mode__c,Neft_Cash__c, Payment_Term_From_Days__c, Payment_Term_To_Days__c 
                        from IncentiveLineItem__c where Incentive__c = : opp[0].IncentiveSlab__c and 
                        Payment_Term_To_Days__c >=: NofoDays and Customer_Mode__c=:opp[0].Customer_Slab_Type__c];

        
            }Else If(opp[0].IncentiveSlab__c!=Null  && Opp[0].Customer_Slab_Type__c=='Type 4')
            {          
              
       
        
                     
            PMT=[select ID from Payment__c  where Project__c=:pay.Project__C and Payment_Type__c='Advance'];
            IF(PMT.size()>0 && Opp[0].Customer_Slab_Type__c=='Type 4')
            {
              lstIli = [select id,Other_Mode__c,Neft_Cash__c, Payment_Term_From_Days__c, Payment_Term_To_Days__c from IncentiveLineItem__c where Incentive__c = : opp[0].IncentiveSlab__c and Discount_From__c<=:PMT[0].Advance_Payment__c and  Discount_To__c>=:PMT[0].Advance_Payment__c and Payment_Term_To_Days__c >=: NofoDays and  Customer_Mode__c=:opp[0].Customer_Slab_Type__c];
            
            }
             Else IF(PMT.size()<=0 && Opp[0].Customer_Slab_Type__c=='Type 4' && opp[0].No_Advance__c==True && NofoDays<=0)
            {
              lstIli = [select id,Other_Mode__c,Neft_Cash__c, Payment_Term_From_Days__c, Payment_Term_To_Days__c from IncentiveLineItem__c where Incentive__c = : opp[0].IncentiveSlab__c and Discount_From__c<=:PMT[0].Advance_Payment__c and  Discount_To__c>=:PMT[0].Advance_Payment__c and Payment_Term_To_Days__c >=: NofoDays and  Customer_Mode__c=:opp[0].Customer_Slab_Type__c];
            
            }
            Else IF(PMT.size()<=0 && Opp[0].Customer_Slab_Type__c=='Type 4' && opp[0].No_Advance__c==True && NofoDays>0)
            {
              lstIli = [select id,Other_Mode__c,Neft_Cash__c, Payment_Term_From_Days__c, Payment_Term_To_Days__c from IncentiveLineItem__c where Incentive__c = : opp[0].IncentiveSlab__c and No_Advance__c=:opp[0].No_Advance__c and Payment_Term_To_Days__c >=: NofoDays and  Customer_Mode__c=:opp[0].Customer_Slab_Type__c];
            
            }
            
            }
            
          if (lstIli.size() > 0) {      
              if (pay.Payment_Mode_Type__c.contains('Cash') || pay.Payment_Mode_Type__c.contains('NEFT'))
              {
                    opp[0].NewIncentivePercentage__c = lstIli[0].Neft_Cash__c;
                     opp[0].No_of_Days__c=NofoDays;
                    }
                else if (pay.Payment_Mode_Type__c.contains('Others'))
                {
                      opp[0].NewIncentivePercentage__c = lstIli[0].Other_Mode__c;
                      opp[0].No_of_Days__c=NofoDays;
                     }
               else if (lstIli.size() < 0) 
               {              
                     opp[0].NewIncentivePercentage__c =0;
                     opp[0].No_of_Days__c=NofoDays;
                }
                list<Opportunity> lstOpp1 = new list<Opportunity>();
                lstOpp1 .add( opp[0]);
                update lstOpp1 ;   
       
           } 
           
           
        Payment__c pay2 = new Payment__c( Amount__c = 8400, Date__c = date.newinstance(2018,6,22), Mode_of_Payment__c = 'Cash',Opportunity__c = Opp[0].id,
        Project__C=proj.ID, Payment_Type__c ='Advance');
        insert pay2;
           
        pay2.Date__c = date.newinstance(2018,6,29);
        Update Pay2;
        
         Target__c  TC= NEw Target__c (Name='Raman.A',Out_Standing_Amount__c=10000,Active__c=True,Target_Amount__c=700000);
        Insert TC; 
        
        Integer numberOfDays;
        Cumulative__c CTC= New Cumulative__c (From_Date__c=Date.today(),
        To_Date__c=date.newinstance(2018,7,30),Target__c=TC.ID);
        
        insert CTC;  
        
        opp[0].Field_Executive__c='Raman.A';
        update Opp[0];
        
        CTC.Type1_Customer_Order_Value_PD__c=1000;
        update CTC;
        
           opp[0].Cumulative_Updated__c=True;
          update Opp[0];
           
       
   }  
   
}



Thanks