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
sekharasekhara 

Help regarding Test Case

My test case is failing . And giving this error

System.ListException: List index out of bounds: 0

Class.Oppctrldelete.save: line 66, column 15 Class.Oppctrldelete_Test.MyTestController: line 62, column 2 External entry point

So please help me to resolve this issue
 
public class Oppctrldelete
{
  
    decimal j = 0;

    public Oppctrldelete(ApexPages.StandardController controller) {


this.id =ApexPages.currentPage().getParameters().get('id');
op =[select id,name,Total_Invoiced__c,Total_Approved__c ,StageFilter__c,final_stage__c ,Quote_vs_Billings_Difference__c ,Filterstage__c,Total_Planned__c,Total_Submitted__c,ownerid,Purchase_order__c,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c from Opportunity where id=:id];

oli = [select id,Quantity,delete__c ,description,pricebookentry.name,UnitPrice,TotalPrice ,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid=:id];
if(op.StageFilter__c== 'Billing-Planned'){
opps =[select id,ownerid,Final_Stage__c,name,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c,Filterstage__c from Opportunity where Header_Opportunity__c =:op.id];
oppli = [select id,Line_Item_Total__c,Quantity,delete__c ,TotalPrice ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps];
}
else if(op.StageFilter__c== 'Billing-Submitted'){
opps =[select id,ownerid,Final_Stage__c,name,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c,Filterstage__c from Opportunity where Header_Opportunity__c =:op.id AND (stagename='Billing-Submitted' OR  stagename='Billing-Approved' OR  stagename='Billing-Invoiced')];
oppli = [select id,Line_Item_Total__c,Quantity,delete__c ,TotalPrice ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps];
}
else if(op.StageFilter__c== 'Billing-Approved'){
opps =[select id,ownerid,Final_Stage__c,name,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c,Filterstage__c from Opportunity where Header_Opportunity__c =:op.id  AND  (stagename='Billing-Approved' OR stagename='Billing-Invoiced')];
oppli = [select id,Line_Item_Total__c,Quantity,delete__c ,TotalPrice ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps];
}
else if(op.StageFilter__c== 'Billing-Invoiced'){
opps =[select id,ownerid,Final_Stage__c,name,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c,Filterstage__c from Opportunity where Header_Opportunity__c =:op.id   AND  stagename='Billing-Invoiced'];
oppli = [select id,Line_Item_Total__c,Quantity,delete__c ,TotalPrice ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps];
}

 //opadd =[select id, MAX(CloseDate)   from Opportunity Limit 1 ];
//oppli = [select id,Line_Item_Total__c,Quantity,delete__c ,TotalPrice ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps];
  Oppsnew = new List<Opportunity>();
  Opplinew = new List<Opportunitylineitem>();
   

   
    }
// Save The Updates on Opp and LineIems
   
   
   
   
    public PageReference save()    
     {
    for(Opportunity oppsk:opps){

if(oppsk.final_stage__c== 'Billing-Planned'){
oppsk.stagename='Billing-Planned';
}
if(oppsk.final_stage__c== 'Billing-Submitted'){
oppsk.stagename='Billing-Submitted';

}
if(oppsk.final_stage__c== 'Billing-Approved'){
oppsk.stagename='Billing-Approved';
}
if(oppsk.final_stage__c== 'Billing-Invoiced'){
oppsk.stagename='Billing-Invoiced';
}
}

        update oppli;
        oppli[0].Fire_Trigger__c = true;
        update Opps;
      //oli[0].Fire_Trigger__c = true;
        update oli;
        
        update op;
       
     
   return(new ApexPages.StandardController(Op)).view();
   
  
  
    }


// Cancel and go Back to Header Opportunity
 public PageReference cancel() {
     return(new ApexPages.StandardController(Op)).view();
        return null;
    }
    
    
    public PageReference addmonthdyn()
     { 
   //alert(opadd.CloseDate);
   integer d;
     integer lkj = opps.size();
     integer poi = lkj-1;
    // oli = [select id,Quantity,delete__c, TotalPrice ,description,pricebookentry.name,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid=:id];
  // Opportunity o =[select id,name,Final_Closed_Date__c,Week_End_Day__c,Estimated_Start_Date__c,Account.name,Year_of_close_date__c,Month_Of_Closedate__c,stagename,CloseDate,pricebook2id,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c from Opportunity where id=:id];
 
    OpportunitylineItem[] oli = [select id,TotalPrice,ListPrice,PricebookEntry.name,Quantity,PricebookEntry.Product2.Name , PricebookEntry.Product2.id, UnitPrice,PricebookEntryId,Opportunityid,Quote_Original_List_Price__c  from OpportunityLineItem where Opportunityid=:id] ;
    opps[poi] =  [select id,Final_Stage__c ,Final_Closed_Date__c,stagename,Description,AccountId ,Hold_My_date__c,Week_End_Day__c ,name,closedate,Estimated_Start_Date__c from Opportunity where Header_Opportunity__c=:op.id ORDER BY closedate DESC LIMIT 1];
   
    Opportunity LitOrd = new Opportunity();
    LitOrd.stagename = 'Billing-Planned';
    LitOrd.Header_Opportunity__c=op.id;     
    LitOrd.recordtypeid='012M000000008mu';
    string mon;    
  
      
    decimal x = (poi+1*30);     
    LitOrd.Hold_My_date__c = opps[poi].closedate +x.intValue();
    
    // LitOrd.closedate = o.closedate;
    // for(Opportunity opps2:opps)
      //{
       //LitOrd.closedate = opps2.closedate+60; 
      // LitOrd.closedate = LitOrd.closedate+30; 
    // }
     LitOrd.closedate = opps[poi].closedate+30; 
    //LitOrd.closedate = opps[poi].closedate;   
   // Integer currentyear = opps[poi].Hold_My_date__c.year();
   // Integer currentMonth = (opps[poi].Hold_My_date__c.Month())+1;
       Integer currentyear =  LitOrd.Hold_My_date__c.year();
       Integer currentMonth =  LitOrd.Hold_My_date__c.Month();
        if(currentMonth==1){
          mon = 'JAN';
          
         
         }
         else if(currentMonth==2){
         mon = 'FEB';
         }
          else if(currentMonth==3){
         mon = 'MAR';
         }
          else if(currentMonth==4){
         mon = 'APR';
         }
          else if(currentMonth==5){
         mon = 'MAY';
         }
          else if(currentMonth==6){
         mon = 'JUN';
         }
          else if(currentMonth==7){
         mon = 'JUL';
         }
          else if(currentMonth==8){
         mon = 'AUG';
         }
          else if(currentMonth==9){
         mon = 'SEP';
         }
          else if(currentMonth==10){
         mon = 'OCT';
         }
          else if(currentMonth==11){
         mon = 'NOV';
         }
          else if(currentMonth==12){
         mon = 'DEC';
         }
        
      LitOrd.accountid = opps[poi].accountid;
      LitOrd.name = op.Account.name+'-'+op.Name+'-'+'Billing'+'-'+mon+'-'+ currentyear;
      
      LitOrd.final_stage__c ='Billing-Planned';
      LitOrd.probability =100;
      LitOrd.amount = 100;
     // LitOrd.closedate = opps[poi].closedate;
      Oppsnew.add(LitOrd); 
      insert oppsnew;
    
    
    /*

      for(j=0;j<oli.size();j++){
         OpportunitylineItem LitOrdch = new OpportunitylineItem();
       for(opportunitylineitem oli1:oli){

        
                  
        // LitOrdch.quantity = oli1.quantity.intValue()/o.No_Of_Months_for_Billing__c.intValue();
      
       //LitOrdch.quantity = oli1.quantity;
     
     
           LitOrdch.quantity=oli1.quantity;
        
         LitOrdch.unitprice = oli1.unitprice; 
     
      // LitOrdch.opportunityid =opps[poi].id;
        LitOrdch.opportunityid =oppsnew[0].id;
       
       
  LitOrdch.PricebookEntryId=oli1.PricebookEntryId;
      LitOrdch.Product_Code__c = oli1.PricebookEntry.name;
      LitOrdch.Quote_Original_Quantity__c =oli1.quantity;
      LitOrdch.Quote_Original_Sale_Price__c =  oli1.unitprice;
      LitOrdch.Quote_Original_List_Price__c = oli1.unitprice;
       
         
       }
         Opplinew.add(LitOrdch); 
        }
    
    insert Opplinew;
    
  */
        PageReference customPage = new PageReference('/apex/opptctldelete'); 
 
   customPage.getParameters().put('id', this.id);
   customPage.setRedirect(true);
   return customPage; 
  
    }
    
    
    public PageReference Addmonth() 
    {
   
    update op;   
   // PageReference customPage = new PageReference('/apex/billingsubhash'); 
 //string i= ApexPages.currentPage().getParameters().get('id');
  PageReference customPage = new PageReference('/apex/redirect'); 
 
   customPage.getParameters().put('id', this.id);
   customPage.setRedirect(true);
   return customPage;
    
    
    }
    
    
    
    
    
    
    
    
  //Quick save changes
  
  
    public PageReference quicksave() {
    
    
     for(Opportunity oppsk:opps){

if(oppsk.final_stage__c== 'Billing-Planned'){
oppsk.stagename='Billing-Planned';
}
if(oppsk.final_stage__c== 'Billing-Submitted'){
oppsk.stagename='Billing-Submitted';

}
if(oppsk.final_stage__c== 'Billing-Approved'){
oppsk.stagename='Billing-Approved';
}
if(oppsk.final_stage__c== 'Billing-Invoiced'){
oppsk.stagename='Billing-Invoiced';
}
}

        update oppli;
        oppli[0].Fire_Trigger__c = true;
        update Opps;
      //oli[0].Fire_Trigger__c = true;
        update oli;
        
        update op;
       
     
   for(opportunity opps1:opps){
    for(Opportunitylineitem oppli1 :oppli){
        if(oppli1.Opportunityid==opps1.id && oppli1.delete__c == true)
        {
        delete oppli1;
            oppli[0].Fire_Trigger__c = true;
       update oli;
       update op;
     }
     }   
   
    }
   
   PageReference customPage = new PageReference('/apex/opptctldelete');   
  customPage.getParameters().put('id', this.id);
  customPage.setRedirect(true);
  return customPage; 
            
    }   
public PageReference incrementCounter() 
{

for(Opportunity opps2:opps){
    for(Opportunitylineitem oppli1:oppli){
    
    
    if(oppli1.OpportunityID == opps2.id   )
  
    {
    

     countindvidual = (oppli1.quantity*oppli1.unitprice);}
    
    
   } 
}  
return null;
}


 public PageReference quan()
   {
    totval=0.0; integer r=0;
    for(opportunity opps1:opps){
    for(Opportunitylineitem oppli1 :oppli)
    {
    if(oppli1.Opportunityid==opps1.id )
    {
      
    }
    }}
    return null;
 }  









  public PageReference childtotal()
   {
    totval=0.0;
    for(opportunity opps1:opps){
    for(Opportunitylineitem oppli1 :oppli)
    {
    if(oppli1.Opportunityid==opps1.id )
    {
     childtot = (oppli1.quantity*oppli1.unitprice)+totval;
     totval=childtot;
    }
    }}
    return null;
 }  
    
   //Get Total of All Billing Amounts 
     public PageReference grandtotal()
 { 
   gtotal=0;
   for(opportunity opps1:opps)
{  
   for(Opportunitylineitem oppli1 :oppli)
    {    
   if(oppli1.Opportunityid==opps1.id)
    {
   
    itotal= (oppli1.quantity*oppli1.unitprice)+gtotal;
    gtotal= itotal;
    // 
  

          }
       
    }
    }return null;
     }
    //Total for Billing Invoiced
    
     public PageReference BI()
 { 
   BItotal=0;
   for(opportunity oppsBI:opps)
{  
   for(Opportunitylineitem oppliBI :oppli)
    {    
   if(oppliBI.Opportunityid==oppsBI.id && oppsBI.final_stage__c =='Billing-Invoiced')
    {
   
    finalBItotal= (oppliBI.quantity*oppliBI.unitprice)+BItotal;
    BItotal= finalBItotal;
    



          }
          else if(oppliBI.Opportunityid==oppsBI.id && oppsBI.final_stage__c =='Billing-Planned'){
          BItotal= finalBItotal;
          }
                    else if(oppliBI.Opportunityid==oppsBI.id && oppsBI.final_stage__c !='Billing-Planned'){
          BItotal= finalBItotal;
          }
BItotal= finalBItotal;
       
    }
    }return null;
     }
   //end of total for billing invoiced
    
   public List<Opportunity> Oppsnew  {get; set;} 
   public List<Opportunitylineitem> Opplinew  {get; set;} 
public Opportunity Op ;
public Opportunity opadd;
public Opportunity[] Opps ;
public OpportunitylineItem[] Oppli ;
public OpportunitylineItem[] Oli ;
public string id;
 decimal gtotal=0.0;
 decimal itotal=0;
user u;
  decimal countindvidual;
  decimal actualcountindvidual;  
    decimal BItotal;
    decimal count = 0; 
  decimal finalBItotal =0;
   decimal counttotal = 0;
   decimal  totval;
   decimal childtot=0.0;
//public Oppctrldelete()
//{

/*
this.id =ApexPages.currentPage().getParameters().get('id');
op =[select id,name,Total_Invoiced__c,Total_Planned__c,Total_Submitted__c,ownerid,Purchase_order__c,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c from Opportunity where id=:id];
oli = [select id,Quantity,delete__c ,pricebookentry.name,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid=:id];
opps =[select id,ownerid,Final_Stage__c,name,Account.name,stagename,CloseDate,amount,probability,Header_Opportunity__c,Accountid,Opp_Line_Items_Count__c,Billing_Contact__c,No_Of_Months_for_Billing__c from Opportunity where Header_Opportunity__c =:op.id];

oppli = [select id,Line_Item_Total__c ,Quantity,delete__c ,description,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in: opps];
//oppli = [select id,Quantity,Description ,Delete__c ,UnitPrice,Opportunityid,Quote_Original_List_Price__c,Product_Code__c   from OpportunityLineItem where opportunityid in:opps or Product_Code__c =: opps[0].name];
*/
//}
public String getname()
{
return 'Oppctrldelete';
}
public Opportunity getop()
{
return op;
}
public user getu()
{
return u;
}
public Opportunity [] getopps()
{
/*
for(Opportunity oppsk:opps){

if(oppsk.final_stage__c== 'BP'){
oppsk.stagename='Billing-Planned';
}
if(oppsk.final_stage__c== 'BS'){
oppsk.stagename='Billing-Submitted';

}
if(oppsk.final_stage__c== 'BA'){
oppsk.stagename='Billing-Approved';
}
if(oppsk.final_stage__c== 'BI'){
oppsk.stagename='Billing-Invoiced';
}
}
*/
return opps;
}






 public PageReference filterstage()
  {
 update op;
   
  PageReference customPage = new PageReference('/apex/opptctldelete');   
  customPage.getParameters().put('id', this.id);
  customPage.setRedirect(true);
  return customPage; 
            
    }   



public OpportunitylineItem[] getoppli()
{

return oppli;
}
public OpportunitylineItem[] getoli()
{

return oli;
}

 
 
  public decimal gettotval()
 {
return totval;
}
public decimal getcountindvidual() {
return countindvidual;
}
 public decimal getgtotal()
 {
return gtotal;
} 
public decimal getBItotal()
 {
return BItotal;
} 
}


/*________------------------------_______*/

@isTest
private class Oppctrldelete_Test
{
static testmethod void MyTestController()
 {
        Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];

        Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
        insert pb;
        
        Product2 prod = new Product2(Name = 'Anti-infectives 2007', Family = 'Best Practices', IsActive = true);
        insert prod;

        PricebookEntry standardPrice = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert standardPrice;

        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 10000, IsActive = true, UseStandardPrice = false);
        insert pbe;
        
 Account a = new Account();
 a.name = 'raja';
 insert a ;
 
  Opportunity opp = new Opportunity();
 opp.Name = 'raja';
 opp.StageName = 'Closedwon';
 opp.StageFilter__c= 'Billing-Planned';
 opp.CloseDate = date.ValueOf('2009-09-21');
 opp.Pricebook2Id = pb.id;
 insert opp;
  
  OpportunitylineItem opli = new OpportunitylineItem  ();
  opli.opportunityid=opp.id;
  opli.TotalPrice=5;
  opli.quantity=3;
  opli.PricebookEntryId = pbe.id; 
  
  insert opli;
       
   update  opli;
   
  
   
  
 ApexPages.currentPage().getParameters().put('id', opp.id);
 ApexPages.StandardController con = new ApexPages.StandardController(opp);
 Oppctrldelete o = new Oppctrldelete(con);
 o.getname();
 o.getopps();
 o.filterstage();
 o.getoppli();
 o.getop();
 O.getu();
 o.getoli();
 o.getcountindvidual();
o.getgtotal();
o.getBItotal();
 o.gettotval();
 o.cancel();
 
 /*---------*/
 o.save();
 o.addmonthdyn();
 
 o.Addmonth();
 o.quicksave();
 o.incrementCounter();
 o.quan();
 o.childtotal();
 
  }
 }

 

Ram-SFRam-SF
oppli[0].

if this list or array does not contains any value, we will get these error

 

check list.size>0

and then proceed

sekharasekhara

public PageReference save()    
     {
    for(Opportunity oppsk:opps){

if(oppsk.final_stage__c== 'Billing-Planned'){
oppsk.stagename='Billing-Planned';
}
if(oppsk.final_stage__c== 'Billing-Submitted'){
oppsk.stagename='Billing-Submitted';

}
if(oppsk.final_stage__c== 'Billing-Approved'){
oppsk.stagename='Billing-Approved';
}
if(oppsk.final_stage__c== 'Billing-Invoiced'){
oppsk.stagename='Billing-Invoiced';
}
}

        update oppli;
        oppli[0].Fire_Trigger__c = true;
        update Opps;
      //oli[0].Fire_Trigger__c = true;
        update oli;
        
        update op;
       
     
   return(new ApexPages.StandardController(Op)).view();
   
  
  
    }
 
I am unable to cover this part in my total code & i am getting 40% code covarage...