6

I am creating a wizard for Opportunity, Quote and QuoteLineItems. I am getting this error

STANDARD_PRICE_NOT_DEFINED, No standard price defined for this product: []

while creating QuoteLineItems..

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

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()
{

    insert Oppo; 
    List<Quote> quoList = new List<Quote>();
    for(Quote q : quoteList)
    {
        Quote quo = new Quote();
        quo.OpportunityId = Oppo.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]);
    List<QuoteLineItem> qliList = new List<QuoteLineItem>();
    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;
    for(Quote q : newQuoteList)
    {
       for(quoteLineItem qli : quoteLineItemList)
       {
        QuoteLineItem qliObj = new QuoteLineItem();
        qliObj.QuoteId= q.id;
        qliObj.PriceBookEntryId = pbe.id;
        qliList.add(qli);
           }
        }      

    insert qliList;
    PageReference pg = new PageReference('/'+oppo.id);
    return pg;
    }           

}

VF Page:

<apex:page controller="WizardClass">
<apex:form >
 <apex:sectionHeader title="OppoQuoteLine"/> 
    <apex:pageBlock >
       <apex:repeat value="{!quoteList}" var="row">
           <apex:outputField value="   {!row.name}" style="font-family :Arial, font-size : 12px" />
           <!--apex:repeat value="{!qliList}" var="r"-->
           <apex:pageBlockSection columns="1">
              <apex:pageBlockTable var="row" value="{!quoteLineItemList}" id="pgt">
                  <!--apex:column headerValue="Price Book Entry">
                      <apex:inputField value="{!row.PriceBookEntryId}"/>
                  </apex:column-->
                  <!--apex:column headerValue="Quote Name">
                      <apex:inputField value="{!row.QuoteId}"/>
                  </apex:column-->
                  <apex:column headerValue="Sales Price">
                      <apex:inputField value="{!row.UnitPrice}"/>
                  </apex:column>
                  <apex:column headerValue="Quantity">
                      <apex:inputField value="{!row.Quantity}"/>
                  </apex:column>
                  <!--apex:column headerValue="Product">
                      <apex:inputField value="{!row.Product2Id}"/>
                  </apex:column-->
              </apex:pageBlockTable>
          <apex:commandButton action="{!CreateQuoteLineItem}" value="Add Row" immediate="true" reRender="pgt"/>

           </apex:pageBlockSection>
           <!--/apex:repeat-->
       </apex:repeat>
       <apex:pageBlockButtons >           
              <apex:commandButton action="{!QuoteLineItemPage}" value="Save"/>
       </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>
</apex:page>

2 Answers 2

9

This error is because you haven't added your product to a standard price book.

To be used in custom price books, all standard prices must be added as price book entries to the standard price book.

More info on Product object

So you can modify your code like that:

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;
// First insert a price for the standard price book
Pricebook2 standardPB = [select id from Pricebook2 where isStandard=true];
PricebookEntry standardPBE = new PricebookEntry(Pricebook2Id = standardPB.Id, Product2Id = prod.Id, UnitPrice = 1000, IsActive = true);
    insert standardPBE;
PricebookEntry pbe = new PricebookEntry(Pricebook2Id = pb.Id, Product2Id = prod.Id, UnitPrice = 1000, IsActive = true);
    insert pbe;
4
  • System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Quote Name, Price Book Entry, Product]: [Quote Name, Price Book Entry, Product] Error is in expression '{!QuoteLineItemPage}' in component <apex:commandButton> in page oppoquoteline: Class.WizardClass.QuoteLineItemPage: line 84, column 1
    – Anzar
    Commented Mar 22, 2016 at 11:13
  • @Anzar, this is pretty obvious as in the error you have all that you are missing and you have the line number where this happens. You have not specified required fields for QuoteLineItem or they are null. This happens because you are adding an empty QuoteLineItem to your list here qliList.add(qli); you add qli variable but modified qliObj in your code. Commented Mar 22, 2016 at 11:55
  • I have corrected that and now a new error is coming System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, The pricebook entry is in a different pricebook than the one assigned to the Quote, or Quote has no pricebook assigned.: [PricebookEntryId] Error is in expression '{!QuoteLineItemPage}' in component <apex:commandButton> in page oppoquoteline: Class.WizardClass.QuoteLineItemPage: line 89, column 1
    – Anzar
    Commented Mar 22, 2016 at 12:22
  • I believe this is because you are not assigning a price book to your quotes in newQuoteList. When you loop trough for(Quote q : newQuoteList) you should add a price book for each Quote. q.Pricebook2Id = pb.Id Commented Mar 22, 2016 at 12:42
9

Since Summer 14, it's been possible to Create Price Book Entries in Apex Tests irrespective of the state of SeeAllData:

Id pricebookId = Test.getStandardPricebookId();

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .