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
Robbi ConverseRobbi Converse 

Select a particular price book on a Visualforce page

I have a Visualforce page that requires users to select a price book when one has not yet been selected for the opportunity. I want to specify which price book is being used. How do I alter the following to specify a pricelist named "Medical"?

<apex:page standardController="Opportunity" extensions="opportunityProductEntryExtension" action="{!priceBookCheck}" >
Best Answer chosen by Robbi Converse
Robbi ConverseRobbi Converse
Thank you again for your reply. I think I am just in over my head. I replaced existing code related to price book with your code, but was not able to test it, because other parts of the Apex class (not related to this part at all) were causing errors - although they had functioned perfectly well before. I am going to take a break and see if it makes more sense to me later.

All Answers

LBKLBK
Do you want to allow the user to choose a PriceBook from a list?

Or, do you want to enforce a (single) PriceBook on the record?

If it is the second option, I guess you can do that in your opportunityProductEntryExtension class itself.
Robbi ConverseRobbi Converse
Thank you for responding! I do want to enforce a (single) PriceBook on the record. How do I do that?
LBKLBK
Here is my sample VF Page and APEX Extension class.
 
<apex:page standardController="Opportunity" extensions="opportunityProductEntryExtension">
   <apex:form >  
       <apex:message />
   <apex:pageBlock title="My Opportunity">
   
       <apex:pageBlockButtons location="bottom">
          <apex:commandButton action="{!Save}" value="Save"/>
       </apex:pageBlockButtons>
   
       <apex:pageBlockSection title="Opportunity Fields" columns="2">
          <apex:inputField value="{!Opportunity.Name}"/>
          <apex:inputField value="{!Opportunity.AccountId}"/>
          <apex:inputField value="{!Opportunity.Amount}"/>
          <apex:inputField value="{!Opportunity.CloseDate}"/>
          <apex:inputField value="{!Opportunity.StageName}"/>
          <apex:outputField value="{!Opportunity.Id}"/>     
       </apex:pageBlockSection>
      </apex:pageBlock>
    </apex:form>
</apex:page>
 
public with sharing class opportunityProductEntryExtension 
{
    ApexPages.StandardController controller;
    public Opportunity oppt {get; set;}
    
    public opportunityProductEntryExtension(ApexPages.StandardController controller)
    {
        this.controller = controller;
        oppt = (Opportunity)controller.getRecord();
		oppt.PriceBook2Id = [SELECT Id FROM PriceBook2 WHERE Name = 'Northwest Diesel Generator Sales'].Id;
    }
    
	public PageReference Save()
	   {
			controller.Save(); 
			return null;
	   }
}

As you can see, I didn't include the PriceBook field in the VF page, because I wanted to enforce it with a specific value in the APEX class.

In the APEX class, I have fetched my "Northwest Diesel Generator Sales" PriceBook and used it as the only PriceBook possible.

It would be "Medical" in your case.

Hope this helps.
Robbi ConverseRobbi Converse
Thank you again for your reply. I think I am just in over my head. I replaced existing code related to price book with your code, but was not able to test it, because other parts of the Apex class (not related to this part at all) were causing errors - although they had functioned perfectly well before. I am going to take a break and see if it makes more sense to me later.
This was selected as the best answer
LBKLBK
You probably need only the following piece from the code above.
 
oppt.PriceBook2Id = [SELECT Id FROM PriceBook2 WHERE Name = 'Northwest Diesel Generator Sales'].Id;
And, ensure that the PriceBook field is not available for the user to change in the VF page.