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
caterina ferrigno 8caterina ferrigno 8 

System.QueryException on a Test Class

Hello all!

I have a problem with a test class (I've never done a test class).
This is my class :
*****************************************************************************************************************
public class CreaNewFatturaCtrl { 
      public Fattura__c fattura {get;set;}    
      private String idQuote;
      private String idQuotelineitem;               
      public List <Wrapper> WrapperList {get;set;}    
      public QuoteLineItem qli1 {get;set;}       
      public id selectedIndex   {get;set;}
      public List<Riga_Fattura__c> ListaRigheFattDaCompilare {get; set;}
      public Id rowToRemove  {get; set;}         
                                                                                                                            
     public CreaNewFatturaCtrl(ApexPages.StandardController stdcontroller) {        
        
        idQuote= ApexPages.currentPage().getParameters().get('idQuote');              
        ListaRigheFattDaCompilare = new list<Riga_Fattura__c>();
        this.fattura=(Fattura__c) stdController.getRecord();        
        fattura.preventivo__c = idQuote;
        fattura.motivo_stato__c = 'Bozza';
        WrapperList = new List <Wrapper>();
                                     
        For(QuoteLineItem ql: (List<QUoteLineItem>) [SELECT Id, LineNumber, ListPrice, UnitPrice, Quantity, Importo_IVA__c, Importo_ancora_d              a_fatturare__c , Totale__c, Description FROM QuoteLineItem WHERE Quote.Id = :idQuote]){            
             Wrapper wp = new Wrapper();
             wp.selected = false;
             wp.ro = ql;
             WrapperList.add(wp);     
        }
     }
                                     
          public ApexPages.PageReference addRow(){
          //selectedIndex = 0;    
          //idQuotelineitem= qli[Integer.ValueOf(selectedIndex)].id; 
          system.debug('*****selectedIndex '+selectedIndex );
          For(Wrapper wp:WrapperList)
            if(wp.ro.id == selectedIndex)
                 wp.selected = true;        
          
          qli1 = [SELECT Id, LineNumber, Importo_ancora_da_fatturare__c, Totale__c,Importo_IVA__c FROM QuoteLineItem WHERE QuoteLineIte                    m.Id = :selectedIndex  LIMIT 1];                                                   
          ListaRigheFattDaCompilare.add(new Riga_Fattura__c(voce_preventivo__c = qli1.id,Voce_preventivo__r = qli1,motivo_stato__c = 'Bozza' ));  
                                       
          return null;                                 
        } 
        
        public void deleteRow(){   
        /*if(ListaRigheFattDaCompilare.size()>=rowToRemove&& rowToRemove> 0) 
          {
                ListaRigheFattDaCompilare.remove(rowToRemove-1);
                rowToRemove= null;
          }*/      
         //ListaRigheFattDaCompilare.remove(ListaRigheFattDaCompilare.size()-1);
         integer i = 0;
         system.debug('***rowToRemove'+rowToRemove);
         system.debug('***prima'+ListaRigheFattDaCompilare);
         For(Wrapper wp:WrapperList)
            if(wp.ro.id == rowToRemove)
                 wp.selected = false;
         For(riga_fattura__c rf : ListaRigheFattDaCompilare){
            if(rf.voce_preventivo__c == rowToRemove)
                break;
            else
                i++;
         }
         ListaRigheFattDaCompilare.remove(i);
         system.debug('***dopo'+ListaRigheFattDaCompilare);                          
        }
                                                                                            
        public ApexPages.PageReference CompilaImporto() {
        for (Riga_Fattura__c rf : ListaRigheFattDaCompilare) {
        
                if (rf.da_fatturare__c  != null  ) {
                 rf.Importo__c = (((rf.Voce_preventivo__r.Totale__c-rf.Voce_preventivo__r.Importo_IVA__c)*rf.da_fatturare__c)/100) ;
                  }
                  }
                   return null;                  
                  }
                                    
        public ApexPages.PageReference CompilaPercentuale() {
        for (Riga_Fattura__c rf : ListaRigheFattDaCompilare) {
        
                if ( rf.Importo__c != null  ) {
                 rf.da_fatturare__c = (rf.Importo__c/(rf.Voce_preventivo__r.Totale__c-rf.Voce_preventivo__r.Importo_IVA__c))*100 ;
                  }
                  }        
                   return null;                      
                  }
                  
            public ApexPages.PageReference saveRecord() {
            insert fattura;
            For(RIga_fattura__c RF:ListaRigheFattDaCompilare)
                RF.fattura__c = fattura.id;
            insert ListaRigheFattDaCompilare;
            
            return new PageReference('/'+fattura.id);
        }
             
        public ApexPages.PageReference cancelRecord() {
            return new PageReference('/'+idQuote);
        }
        
        public class Wrapper{
            public boolean selected{get;set;}
            public quotelineitem ro{get;set;}
            public Wrapper(){
                
            } 
        }
                  
}
************************************************************************************************************************

And this is my incomplete test class:
************************************************************************************************************************
@isTest

public class CreaNewFatturaCtrl_Test {

    static testMethod void Costruttore1_Test() {
      test.startTest();
           
      Account acc = new Account();
      acc.name='Test Account';
      acc.billingStreet='via emanuele';
      acc.billingCity='Torino';
      acc.billingState='Ecuador';
      acc.billingCountry='Italy';
      insert acc;
            
      List<Opportunity> l_opp = new List<Opportunity>();  
      Opportunity opp = new Opportunity();
      opp.Accountid = acc.id;
      opp.Name = 'test';
      opp.StageName = 'Prospecting';
      opp.CloseDate = date.today();
      opp.Type = 'New Client';
      opp.NextStep = 'Test';
      opp.LeadSource = 'Business Development';
      insert opp;
      l_opp.add(opp);
            
      Product2 prod = new Product2 ();
      prod.name='test';
      prod.IsActive=true;
      prod.Ha_Edizioni__c = true;
      insert prod;
      List<Product2> l_prod = new List<Product2>();
      l_prod.add(prod);
      
      Id pricebookId = Test.getStandardPricebookId();
      List<PricebookEntry> l_pbe = new List<PricebookEntry>();
      PricebookEntry pbe = new PricebookEntry();
      pbe.Product2Id=prod.Id;
      pbe.IsActive=true;
      pbe.UnitPrice=1;
      pbe.Pricebook2Id=pricebookId;
      insert pbe;
      l_pbe.add(pbe);
                              
      List<Quote> l_off = new List<Quote>();      
      Quote preventivo = new Quote();
      preventivo.Name = 'soldi';
      preventivo.N_Offerta__c = '9';
      preventivo.OpportunityId = opp.id;
      preventivo.Pricebook2Id = pricebookId ;
      preventivo.Training__c = TRUE;
      insert preventivo ; 
      l_off.add(preventivo);
            
      Fattura__c fat = new Fattura__c(Motivo_stato__c='bozza');      
      fat.Preventivo__c = preventivo.id;      
      insert fat;
      List<Fattura__c> l_fat = new List<Fattura__c>();
      l_fat.add(fat);
            
      Date data = date.newInstance(2999,02,36); 
      Edizione__c edz = New Edizione__c();
      edz.Name = 'Test';
      edz.Prodotto__c = prod.id;
      edz.motivo_stato__c='edizione erogata';
      insert edz;
      List<Edizione__c> l_edz = new List<Edizione__c>();
      l_edz.add(edz);
          
      List<QuoteLineItem> l_qtl = new List<QuoteLineItem>();
      QuoteLineItem qtl = new QuoteLineItem();
      qtl.quoteid = preventivo.id;
      qtl.quantity = 1;
      qtl.unitprice = 1;
      qtl.PricebookEntryId = pbe.id;      
      qtl.Product2Id = prod.id;
      qtl.Edizione__c=edz.id;
      insert qtl;
      l_qtl.add(qtl);
            
     /*** Fattura__c fat = new Fattura__c(Motivo_stato__c='bozza');      
      fat.Preventivo__c = preventivo.id;      
      insert fat;
      List<Fattura__c> l_fat = new List<Fattura__c>();
      l_fat.add(fat); ***/
            
      List<Riga_Fattura__c> l_riga_f = new List<Riga_Fattura__c>(); 
      Riga_Fattura__c riga_f = new Riga_Fattura__c ();
      riga_f.Motivo_stato__c='Bozza';
      riga_f.Fattura__c = fat.id;
      riga_f.voce_preventivo__c= qtl.id;
      insert riga_f;
                                              
      boolean bl = true; 
      quotelineitem quolitem = new quotelineitem();              
              quolitem.quoteid = preventivo.Id;
              quolitem.UnitPrice = 600;
              quolitem.Quantity = 5;
              quolitem.PricebookEntryId = pbe.id;
              quolitem.Product2Id = prod.Id;  
      CreaNewFatturaCtrl.Wrapper cnf = new CreaNewFatturaCtrl.Wrapper();
             
         ApexPages.StandardController controller = new ApexPages.StandardController(fat);
        CreaNewFatturaCtrl stdController = new CreaNewFatturaCtrl(controller);
                       
         stdController.addRow();     
         stdController.deleteRow();
         stdController.CompilaImporto();
         stdController.CompilaPercentuale();   
         stdController.saveRecord();
         stdController.cancelRecord(); 
          
   test.stopTest();
   } 
                  
 }
************************************************************************************************************************

When I run the test I have this error:

System.QueryException: List has no rows for assignment to SObject
Traccia dello stackClass.CreaNewFatturaCtrl.addRow: line 41, column 1
Class.CreaNewFatturaCtrl_Test.Costruttore1_Test: line 122, column 

And I think there's the same problem for other methods!
Is there anyone who can help me?
Thanks!
karthikeyan perumalkarthikeyan perumal
Hello

Check weather this query returns something, or its null,  
qli1 = [SELECT Id, LineNumber, Importo_ancora_da_fatturare__c, Totale__c,Importo_IVA__c FROM QuoteLineItem WHERE QuoteLineIte  m.Id = :selectedIndex  LIMIT 1];
so that you will get error while adding to list, 
 
ListaRigheFattDaCompilare.add(new Riga_Fattura__c(voce_preventivo__c = qli1.id,Voce_preventivo__r = qli1,motivo_stato__c = 'Bozza' ));
use below  statement so know what it returns.
System.Debug(qli1 );
System.Debug(ListaRigheFattDaCompilare);

make this query return some result. 

Hope this help you. 

Thanks
karthik




           
caterina ferrigno 8caterina ferrigno 8
USER_DEBUG [36]|DEBUG|*****selectedIndex 0QL4E0000004VZLWA2
USER_DEBUG [37]|DEBUG|null
USER_DEBUG [38]|DEBUG|()
 
Amit Chaudhary 8Amit Chaudhary 8
Update your apex class like below
public class CreaNewFatturaCtrl 
{ 
      public Fattura__c fattura {get;set;}    
      private String idQuote;
      private String idQuotelineitem;               
      public List <Wrapper> WrapperList {get;set;}    
      public QuoteLineItem qli1 {get;set;}       
      public id selectedIndex   {get;set;}
      public List<Riga_Fattura__c> ListaRigheFattDaCompilare {get; set;}
      public Id rowToRemove  {get; set;}         
                                                                                                                            
     public CreaNewFatturaCtrl(ApexPages.StandardController stdcontroller) 
	 {        
        idQuote= ApexPages.currentPage().getParameters().get('idQuote');              
        ListaRigheFattDaCompilare = new list<Riga_Fattura__c>();
        this.fattura=(Fattura__c) stdController.getRecord();        
        fattura.preventivo__c = idQuote;
        fattura.motivo_stato__c = 'Bozza';
        WrapperList = new List <Wrapper>();
        For(QuoteLineItem ql: (List<QUoteLineItem>) [SELECT Id, LineNumber, ListPrice, UnitPrice, Quantity, Importo_IVA__c, Importo_ancora_d              a_fatturare__c , Totale__c, Description FROM QuoteLineItem WHERE Quote.Id = :idQuote])
		{          
             Wrapper wp = new Wrapper();
             wp.selected = false;
             wp.ro = ql;
             WrapperList.add(wp);     
        }
     }
                                     
          public ApexPages.PageReference addRow()
		  {
          //selectedIndex = 0;    
          //idQuotelineitem= qli[Integer.ValueOf(selectedIndex)].id; 
          system.debug('*****selectedIndex '+selectedIndex );
          For(Wrapper wp:WrapperList)
            if(wp.ro.id == selectedIndex)
                 wp.selected = true;        
          
          List<QuoteLineItem> Listqli1 = [SELECT Id, LineNumber, Importo_ancora_da_fatturare__c, Totale__c,Importo_IVA__c FROM QuoteLineItem WHERE QuoteLineIte                    m.Id = :selectedIndex  LIMIT 1];  

		  if(Listqli1.size() > 0 )
		  {	
			qli1 = Listqli1[0];
			ListaRigheFattDaCompilare.add(new Riga_Fattura__c(voce_preventivo__c = qli1.id,Voce_preventivo__r = qli1,motivo_stato__c = 'Bozza' ));  
          }                             
          return null;                                 
        } 
        
        public void deleteRow(){   
        /*if(ListaRigheFattDaCompilare.size()>=rowToRemove&& rowToRemove> 0) 
          {
                ListaRigheFattDaCompilare.remove(rowToRemove-1);
                rowToRemove= null;
          }*/      
         //ListaRigheFattDaCompilare.remove(ListaRigheFattDaCompilare.size()-1);
         integer i = 0;
         system.debug('***rowToRemove'+rowToRemove);
         system.debug('***prima'+ListaRigheFattDaCompilare);
         For(Wrapper wp:WrapperList)
            if(wp.ro.id == rowToRemove)
                 wp.selected = false;
         For(riga_fattura__c rf : ListaRigheFattDaCompilare){
            if(rf.voce_preventivo__c == rowToRemove)
                break;
            else
                i++;
         }
         ListaRigheFattDaCompilare.remove(i);
         system.debug('***dopo'+ListaRigheFattDaCompilare);                          
        }
                                                                                            
        public ApexPages.PageReference CompilaImporto() {
        for (Riga_Fattura__c rf : ListaRigheFattDaCompilare) {
        
                if (rf.da_fatturare__c  != null  ) {
                 rf.Importo__c = (((rf.Voce_preventivo__r.Totale__c-rf.Voce_preventivo__r.Importo_IVA__c)*rf.da_fatturare__c)/100) ;
                  }
                  }
                   return null;                  
                  }
                                    
        public ApexPages.PageReference CompilaPercentuale() {
        for (Riga_Fattura__c rf : ListaRigheFattDaCompilare) {
        
                if ( rf.Importo__c != null  ) {
                 rf.da_fatturare__c = (rf.Importo__c/(rf.Voce_preventivo__r.Totale__c-rf.Voce_preventivo__r.Importo_IVA__c))*100 ;
                  }
                  }        
                   return null;                      
                  }
                  
            public ApexPages.PageReference saveRecord() {
            insert fattura;
            For(RIga_fattura__c RF:ListaRigheFattDaCompilare)
                RF.fattura__c = fattura.id;
            insert ListaRigheFattDaCompilare;
            
            return new PageReference('/'+fattura.id);
        }
             
        public ApexPages.PageReference cancelRecord() {
            return new PageReference('/'+idQuote);
        }
        
        public class Wrapper{
            public boolean selected{get;set;}
            public quotelineitem ro{get;set;}
            public Wrapper(){
                
            } 
        }
                  
}
Let us know if this will help you
 
caterina ferrigno 8caterina ferrigno 8
Now I have this error message:

System.ListException: List index out of bounds: 0

Traccia dello stackClass.CreaNewFatturaCtrl.deleteRow: line 70, column 1
Class.CreaNewFatturaCtrl_Test.Costruttore1_Test: line 126, column 1