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
Matt1000Matt1000 

Unable to access PriceBookEntry object, getPriceBookEntry()

Not sure how to go about this. There does not seem to be a getPriceBookEntry() getter built in.

 

When I attempt to reference the PriceBookVariable named entry, I get an error "Unknown Property."

 

In short, I want to look up price book entries and reference them on forms to add new products to an opportunity.

 

Visual Force:

 

<apex:page controller="addProductToOp002">
        <apex:form >
    <apex:pageBlock title="Retrieving Query String Parameters">
        You are viewing the {!opportunity.name} opportunity.
<br />
<br />
PriceBook2 = {!pricebook2.id}
<br />
<br />
PriceBookEntry = {!entry} // causes Unknown Property error
<br />
<br />
OpportunityLineItem data (Referenced from Add Products to Opportunity form):
<br />
OpportunityID = {!opportunitylineitem.OpportunityID}.
<br />
PriceBookEntryID = {!opportunitylineitem.PriceBookEntryID}.
<br />
Quantity = {!opportunitylineitem.Quantity}.
<br />
UnitPrice = {!opportunitylineitem.UnitPrice}.
<br />
<apex:inputField id="Quantity01uE0000000d3Y8" value="{!opportunitylineitem.Quantity}"/>
      <apex:pageBlockButtons location="bottom">
        <apex:commandButton action="{!save}" value="Save"/>
      </apex:pageBlockButtons>

    </apex:pageBlock>
            </apex:form>
    </apex:page>

 

Apex Class:

 

public class addProductToOp002 {

  Opportunity opportunity;

  public Opportunity getOpportunity() {
        opportunity = [SELECT id, name, PriceBook2Id FROM Opportunity
                WHERE id = :ApexPages.currentPage().getParameters().get('id')];
        return opportunity;
    }
    
    PriceBook2 pricebook2;

    public PriceBook2 getPriceBook2() {    
        //pricebook2 = [SELECT id, name FROM PriceBook2 WHERE IsActive = true LIMIT 1];
        pricebook2 = [SELECT id, name FROM PriceBook2 WHERE id=:opportunity.PriceBook2Id LIMIT 1];
        return pricebook2;
    }

    PriceBookEntry entry;
    
    public PriceBookEntry getPriceBookEntry() {
        entry = [Select Id, name from PriceBookEntry where pricebook2id = :opportunity.PriceBook2Id LIMIT 1];
        return entry;
    }

    
    OpportunityLineItem opportunitylineitem;
    
    public OpportunityLineItem getOpportunityLineItem() {
        if(opportunitylineitem == null) opportunitylineitem = new OpportunityLineItem();

// QueryResult qr = binding.query("Select Id, name from PriceBook where IsActive = true");

        opportunitylineitem.OpportunityID = ApexPages.currentPage().getParameters().get('id');

        //01uE0000000d3Y8 is PriceBookEntryID from HTML <form> on Add Products to Opportunity page
        opportunitylineitem.PriceBookEntryID = '01uE0000000d3Y8';
        opportunitylineitem.Quantity = 3;
        opportunitylineitem.UnitPrice = 5000;

        return opportunitylineitem;
   }
    
   public PageReference save() {

      // Add the opportunity line item to the database.       
      insert opportunitylineitem;

      // Send the user to the detail page for the new account.      
      PageReference oppPage = new ApexPages.StandardController(opportunity).view();
      oppPage.setRedirect(true);

      return oppPage;
   }
    
}

 

 

Matt1000Matt1000

I believe I have solved this...

 

 

Instead of this:

 

    PriceBookEntry entry;


    public PriceBookEntry getPriceBookEntry() {
        entry = [Select Id, name from PriceBookEntry LIMIT 1];
        return entry;
    }

 

I used this:

 

    PriceBookEntry entry;


    public PriceBookEntry getEntry() {
        entry = [Select Id, name from PriceBookEntry LIMIT 1];
        return entry;
    }

 

As a newbie, I am still unsure why the getter did not work for getPriceBookEntry(), and why they appear to exist for getOpportunity() and getPriceBook2().

 

I welcome feedback, warnings, or advice, but I am moving forward again.

 

Thanks, Matt