Skip to main content Join the Agentforce Virtual Hackathon to build innovative solutions and compete for a $50k Grand Prize. Sign up now. Terms apply.
Hi, I'm trying to write test class for apex code. The results of run test is:  System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

My apex class is :

public class ImportoFatturato {

   

    public static void ImportoFatturatoTrigger(list<Riga_Fattura__c> LRF){

        set<id> RFIds = new set<id>();

        list<QuoteLineItem> LQLI = new list<QuoteLineItem>();

        For(Riga_Fattura__c RF:LRF) 

             RFIds.add(RF.id);  

        LRF = [Select id,Totale_riga_fattura__c,voce_preventivo__c,voce_preventivo__r.Importo_fatturato__c from riga_fattura__c where id in:RFIds];

        for(Riga_fattura__c r: LRF) {

            if(r.voce_preventivo__r.Importo_fatturato__c != null)

                r.voce_preventivo__r.Importo_fatturato__c += r.Totale_riga_fattura__c;

            else

                r.voce_preventivo__r.Importo_fatturato__c = r.Totale_riga_fattura__c;

            LQLI.add(r.voce_preventivo__r);

        }

        update LQLI;

    }

}

This class retrived an apex trigger :

trigger RigaFatturaTrigger on Riga_Fattura__c (before insert, before update, after insert, after update, after delete, after undelete) {

    

    if (trigger.isAfter){

        FatturaHelper.StatusHandler();

        if (trigger.isUpdate){

           

            list<Riga_Fattura__c> LRF = new list<Riga_Fattura__c>();

            for(Id idRF : Trigger.newMap.keyset()){

                if(Trigger.newMap.get(idRF).motivo_stato__c == 'Inviata' && Trigger.newMap.get(idRF).motivo_stato__c != Trigger.oldMap.get(idRF).motivo_stato__c) 

                       LRF.add(Trigger.newMap.get(idRF));             

            }

            ImportoFatturato.ImportoFatturatoTrigger(LRF);

        }

    }

test class is: 

@isTest(SeeAllData=true)

public class ImportoFatturato_Test {

    static testMethod void Costruttore_Test()

    { 

/******************************************************************* INIZIO DEL TEST   ***********************************************************************************************/

  test.startTest();

        

     Account acc = new Account(name='Test Account', billingStreet='Test', billingCity='Test',billingState='Test', billingCountry='Test');

     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);

     

     List <Pricebook2> Lpb2 = new List <Pricebook2>();

     Pricebook2 pb2 = new Pricebook2 ();

     pb2.name='test';

     pb2.IsActive=true;

     insert pb2;

     Lpb2.add(pb2);

        

     Product2 p2 = new Product2 ();

     List <Product2> Lp2 = new List <Product2>();

     p2.name='test';

     p2.IsActive=true;

     insert p2;

     Lp2.add(p2);

        

     List <PricebookEntry> Lpbe = new List <PricebookEntry>();

     PricebookEntry pbe = new PricebookEntry ();

     pbe.Product2Id=p2.Id;

     pbe.IsActive=true;

     pbe.UseStandardPrice=true;

     pbe.UnitPrice=1;

     pbe.Pricebook2Id=pb2.Id;

     insert pbe; 

     Lpbe.add(pbe);

     

     Quote preventivo = new Quote(Name = 'Test',OpportunityId = opp.id, N_Offerta__c = 'ciao');

     insert preventivo;

     

      quoteLineItem qtl = new quotelineitem(quoteid = preventivo.id, quantity=1, unitprice=1, PricebookEntryId=pbe.id); 

      insert qtl; 

      

      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='Inviata';

      riga_f.Voce_preventivo__c='01';

      l_riga_f.add(riga_f);

        

          test.stopTest();

/******************************************************************* FINE DEL TEST   ***********************************************************************************************/    

   }

 }

How I can  resolve this problem? Help me in write this test class.

Thank you very much.

 
3 respostas
  1. 15 de set. de 2016, 08:39
    I have solved thank you :) the test class is:

    @isTest

    public class ImportoFatturato_Test

    {

        static testMethod void Costruttore_Test()

        {

            Account acc = new Account(name='Test Account', billingStreet='Test', billingCity='Test',billingState='Test', billingCountry='Test');

            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);

     

        test.startTest();

       

         Product2 p2 = new Product2 ();

         List <Product2> Lp2 = new List <Product2>();

         p2.name='test';

         p2.IsActive=true;

         insert p2;

         Lp2.add(p2);

            

         Id pricebookId = Test.getStandardPricebookId();

         List <PricebookEntry> Lpbe = new List <PricebookEntry>();

         PricebookEntry pbe = new PricebookEntry ();

         pbe.Product2Id=p2.Id;

         pbe.IsActive=true;

         pbe.UnitPrice=1;

         pbe.Pricebook2Id=pricebookId;

         insert pbe;

         Lpbe.add(pbe);

         Quote preventivo = new Quote(Name = 'Test',OpportunityId = opp.id, N_Offerta__c = 'ciao', Pricebook2Id=pricebookId );

         insert preventivo;

          QuoteLineItem qtl = new quotelineitem(quoteid = preventivo.id, quantity=1, unitprice=1, PricebookEntryId=pbe.id, Product2Id=p2.id);

          insert qtl;

           

          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='Inviata1';

          riga_f.Voce_preventivo__c=qtl.id;

          insert riga_f;

            

          riga_f.Motivo_stato__c='Bozza';

          update    riga_f;

         

          l_riga_f.add(riga_f);

            

          test.stopTest();

       }

     }

    thank you very much for your help 

     
Carregando
0/9000