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 

Apex Code deployment Error From Sandbox to production?

Hi,

I wrote a trigger and test Class. am getting 100% coverage in the sandbox but 0% Code Coverage in Production. Kindly Please help me to solve this issue. 

My trigger:

trigger Cumulative_Calcuation 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)
Public class Cum_incentive_Test{
    static testMethod void Cum_incentive_Test(){
    
    String opportunityName = 'My Opportunity';
String standardPriceBookId = '';
list<Cumulative__c > lstcum = new list<Cumulative__c >(); 
list<Opportunity > opplist = new list<Opportunity >(); 


 Incentive__c INC= 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 INC;
        
        IncentiveLineItem__c inclitem = new IncentiveLineItem__c(Incentive__c = INC.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);
        insert inclitem;
PriceBook2 pb2Standard = [select Id ,Incentive_Slab__c from Pricebook2 where isStandard=true];
standardPriceBookId = pb2Standard.Id;

pb2Standard .Incentive_Slab__c=inc.ID;
Update pb2Standard ;

 Account a = new Account(Name = 'ApprovalTest' ,Lock_record__C=FALSE , Customer_Type__c='Builders');
 insert a; 
 
 Contact con = new COntact();
 con.email='test@fmail.co';
 con.ACCOUNTID=a.ID;
 con.lastname='test' ;            
 insert con;   
    
Opportunity o = new Opportunity(AccountId=null, Name='test', 
StageName='Prospecting', CloseDate=Date.today());
insert o;

Opportunity opp = [SELECT Name,IncentiveSlab__c,Discount__c FROM Opportunity WHERE Id = :o.Id];


Product2 p2 = new Product2(Name='Test Product',isActive=true);
insert p2;
Product2 p2ex = [SELECT Name FROM Product2 WHERE Id = :p2.Id];
System.assertEquals('Test Product', p2ex.Name);


PricebookEntry pbe = new PricebookEntry(Pricebook2Id=standardPriceBookId, Product2Id=p2.Id, UnitPrice=99, isActive=true);
insert pbe;
PricebookEntry pbeex = [SELECT Pricebook2Id FROM PricebookEntry WHERE Id = :pbe.Id];
System.assertEquals(standardPriceBookId, pbeex.Pricebook2Id);

OpportunityLineItem oli = new OpportunityLineItem(PriceBookEntryId=pbe.Id, OpportunityId=o.Id, Quantity=1, TotalPrice=99);
insert oli;

OpportunityLineItem oliex = [SELECT PriceBookEntryId FROM OpportunityLineItem WHERE Id = :oli.Id];
System.assertEquals(pbe.Id, oliex.PriceBookEntryId); 

Quote Q = new Quote();
            Q.Name = 'Quote';
            Q.OpportunityId = Opp.id; 
            Q.Quote_Count__c = 00000000;
            Q.Approved__c = true;
            Q.Quote_Number__c ='000000';
            Q.Quotation_Date__c = date.newinstance(2013,3,5);
            Q.Pricebook2Id = pb2Standard .id;        
            insert Q;
   
Project__c Proj = new Project__c(name='Proj't, Opportunity__c = Opp.id, Quote__c = Q.id, Project_Type__c = 'New PE', Date_of_Installation__c = date.newinstance(2013,4,5),
Nature_of_Case__c ='No Adv, Dspch & Erection Pending',Contact__C=con.ID,Customer_Name__c=a.id,Special_Instructions__c ='Test for Work order Pdf to be generated');    
            
insert Proj;  

 

Opp.Pricebook2Id = pb2Standard .id;
opp.IncentiveSlab__c= pb2Standard .Incentive_Slab__c;
opp.Field_Executive__c='Raman.A';
update Opp; 

list<Incentive__c > lstInc1=new list<Incentive__c >{INC};


/*lstInc1=[select ID from Incentive__c where ID=:Opp.IncentiveSlab__c AND Opp_Discount_To__c>=:opp.Discount__c];
      
update lstInc1;*/
 
    
Target__c  TC= NEw Target__c (Name='Raman.A',Out_Standing_Amount__c=10000,Active__c=True,Target_Amount__c=700000);
 Insert TC; 
 
 

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;  

  
Target__c  TC1= NEw Target__c (Name='Ashok',Out_Standing_Amount__c=10000,Active__c=True,Target_Amount__c=700000);
 Insert TC1; 
 
 
Cumulative__c CTC1= New Cumulative__c (From_Date__c=Date.today(),
To_Date__c=date.newinstance(2018,7,30),Target__c=TC1.ID);
  
insert CTC1; 

    
Opportunity o1 = new Opportunity(AccountId=null, Name='test', 
StageName='Open', CloseDate=Date.today());
insert o1;

Opportunity o2 = new Opportunity(AccountId=null, Name='test', 
StageName='Open', CloseDate=Date.today());
insert o2;

o2.StageName='Closed/Lost';

Update o2;


Opportunity o3 = new Opportunity(AccountId=null, Name='test', 
StageName='Open', CloseDate=Date.today());
insert o3;
 try{ 
          o3.StageName='Closed Won';
          Update o3;
                   
        }Catch (DMLException e){
            System.assert(e.getMessage().contains('No Test Record'), 'No Test Record.');
        }




        
}
}


Thanks,
Shakila
 
Best Answer chosen by shakila G
Niraj Kr SinghNiraj Kr Singh
Hi shakila G,

You can try your test class with @isTest(seeAlldata=False) OR @isTest . Both are same.

Thanks
Niraj