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
Ravi Dutt SharmaRavi Dutt Sharma 

Associated price book for an opportunity

How to find associated price book for a given opportunity. For example, when I add a product under opportunity for the first time, it asks me to select a price book. Now in my apex code, I need to check whether a given opportunity already has a related price book or not?

User-added image
Ravi Dutt SharmaRavi Dutt Sharma
PS : I have the opportunity id and product id with me
SELECT Product2Id FROM OpportunityLineItem WHERE OpportunityId=oppIdFromURL'
SELECT Pricebook2.Name FROM PricebookEntry WHERE Product2Id='ProductIdFromFirstStep'
Balaji BondarBalaji Bondar
Hi Ravi,

You can query the Opportunity object and get the PricebookId to find out if existing pricebook is there related to Opportunity.
Pricebook2Id
ID of a related Pricebook2 object. The Pricebook2Id field indicates which Pricebook2 applies to this opportunity. The Pricebook2Id field is defined only for those organizations that have products enabled as a feature. You can specify values for only one field (Pricebook2Id or PricebookId)—not both fields. For this reason, both fields are declared nillable.
http://www.salesforce.com/developer/docs/api/Content/sforce_api_objects_opportunity.htm

Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
Ravi Dutt SharmaRavi Dutt Sharma
Thanks Balaji. Cant I use this field while creating a button which executes JS code onClick event
 
if ({!Opportunity.Pricebook2Id} != null) {
window.open("/apex/Display_PriceBooks_on_Opp","_self");
} else {
window.open("/apex/AddProduct","_self");
}
This gives me complilation error that
Error: Field Opportunity.Pricebook2Id does not exist. Check spelling
User-added image