-1

Controller:

public class WizardClass 
{
      public Opportunity Oppo{get;set;}
      public List<Quote> quoteList{get;set;}
      public List<QuoteLineItem> quoteLineItemList{get;set;}
      public List<Quote> newQuoteList{get;set;}
      Pricebook2 standardPB;

    public WizardClass()
    {
        Oppo = new Opportunity();
        quoteList = new List<Quote>();
        quoteList.add(new quote());
        quoteLineItemList = new List<QuoteLineItem>();
        quoteLineItemList.add(new QuoteLineItem());
        }

    public void CreateQuote()
    {
        quoteList.add(new quote());
        }

    public void CreateQuoteLineItem()
    {
        quoteLineItemList.add(new QuoteLineItem());
        }

    public PageReference QuotePage()
    {
        return Page.OppoQuotes;
        }


    public PageReference Create()
    {
        //Pricebook2 pb = [Select id, name From Pricebook2 Where name ='Standard Edition'];
        standardPB = [select id from Pricebook2 where name='Standard' And IsActive=true limit 1];
        Oppo.Pricebook2Id = standardPB.Id;
        insert Oppo; 

        List<Quote> quoList = new List<Quote>();
        for(Quote q : quoteList)
        {
            Quote quo = new Quote();
            quo.OpportunityId = Oppo.id;
            quo.Pricebook2Id = standardPB.Id;
            quo.name = q.name;
            quoList.add(quo);
            }
       // q.OpportunityId = Oppo.id;
        insert quoList;
        newQuoteList = quoList;
        PageReference newPage = new PageReference('/apex/OppoQuoteLine');
        return newPage;
        }

     public PageReference QuoteLineItemPage()
     {
       // Map<String,Quote> mapping = new Map<String,Quote>([Select id, name, (Select id, name From QuoteLineItem) From Quote]);

      //  Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true);
     //   insert pb;

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

      /*  Pricebook2 pb = new Pricebook2(Name = 'Standard Price Book 2009', Description = 'Price Book 2009 Products', IsActive = true );
        insert pb; 
        Product2 prod = new Product2(Name = 'SLA: Bronze', IsActive = true);
        insert prod;
        PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 1000, IsActive = true);
        insert pbe;*/



        /*Map<Id,Product2> mapProduct = new Map<Id,Product2>([Select id, name, Family, ProductCode From Product2]);
        for(Product2 pro : [Select id from Product2])

            PricebookEntry pbe = new PricebookEntry();
            pbe.Pricebook2Id = pb.Id;
            pbe.Product2Id = mapProduct.get(
            UnitPrice = 1000; 
            IsActive = true;
            insert pbe;*/

        List<QuoteLineItem> qliList = new List<QuoteLineItem>();
        for(Quote q : newQuoteList)
        {
            system.debug('newQuoteList'+newQuoteList);
            system.debug('newQuoteList'+q);
           for(quoteLineItem qli : quoteLineItemList)
           {
           // QuoteLineItem qliObj = new QuoteLineItem();
            qli .QuoteId= q.id;
            qli .PriceBookEntryId = setPriceBookEntry(standardPB.id,qli.Product2Id);
            system.debug('qliobj'+qli );
            qliList.add(qli );
            system.debug('qli'+qliList);
               }
            }      
        insert qliList;
        PageReference pg = new PageReference('/'+oppo.id);
        return pg;
        }           

         public ID setPriceBookEntry(ID stPBID, ID pid){

            PricebookEntry pbe = new PricebookEntry(Pricebook2Id = stPBID, 
                                                    Product2Id = pid, 
                                                    UnitPrice = 1000, 
                                                    IsActive = true);
            insert pbe;
             return pbe.id;
        }        

}

Test Class:

@isTest  
Private class WizardClassTest
{  
      static testMethod void unitTest()   
     {    
        Test.startTest() ; 

        PriceBook2 pb = new PriceBook2();
        pb.Name = 'New PriceBook';
        insert pb;

        Opportunity opp = new opportunity();  
        opp.Name = 'Roy';  
        opp.CloseDate = system.today();  
        opp.StageName = 'Prospecting' ;  
        opp.Pricebook2Id = pb.Id;
        //Insert opp;  

        List<Quote> quoteList = new List<Quote>();  
        Quote q = new Quote();  
        q.name = 'Joe';  
        q.opportunityId = opp.Id;  
        q.Pricebook2Id = pb.Id;
        quoteList.add(q);  

        Quote quo = new Quote();  
        quo.name = 'Monroe';  
        quo.opportunityId = opp.Id;
        q.Pricebook2Id = pb.Id;  
        quoteList.add(quo);  
        //insert quoteList; 

        Product2 pro = new Product2();
        pro.Name = 'Go Pro';

        insert pro;

        PriceBookEntry pbe = new PriceBookEntry(Pricebook2Id = pb.id, 
                                                    Product2Id = pro.Id, 
                                                    UnitPrice = 1000, 
                                                    IsActive = true);
        insert pbe;

        List<QuoteLineItem> qli = new List<QuoteLineItem>();
        QuoteLineItem ql = new QuoteLineItem();
        ql.QuoteId = q.id;
        ql.PriceBookEntryId = pbe.Id;
        qli.add(ql);

        WizardClass wc = new WizardClass();
        wc.oppo = opp;
        wc.quoteList = quoteList;
        wc.Create();
        wc.CreateQuote();
        wc.QuotePage();
        wc.quoteLineItemList = qli;
        wc.QuoteLineItemPage();


        //System.assertEquals(2,opp.quotes.size());  

        Test.stopTest();
    }  
 }

Method is failing 0% coverage is coming.I dont know whats happening..

  • 4
    We don't as well. Would you share with us more details about failure? – kurunve Apr 4 '16 at 10:38
  • what is the error? – unidha Apr 4 '16 at 10:47
  • System.DmlException: Insert failed. First exception on row 0; first error: STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: [] – Anzar Apr 4 '16 at 10:53
1

You need to define standard price book like below code.

Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];

PriceBookEntry pbe = new PriceBookEntry(Pricebook2Id = standardPB.id, 
                                                Product2Id = pro.Id, 
                                                UnitPrice = 1000, 
                                                IsActive = true,   UseStandardPrice = false);
insert pbe;
  • 1
    why don't you just use Test.getStandardPricebookId()? Why re-inventing the wheel? – Novarg Apr 4 '16 at 14:09
  • It worked..Thanx man... – Anzar Apr 5 '16 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.