[SalesForce] How to check is Price Book added in the Opportunity or not in, Apex class, Salesforce

Description : I am overriding the functionality of Add Product button through code (which is present in related list of Opportunity, in Opportunity Line Item.)…i need to check is Price Book added for the current Opportunity or not, how can I check it in my code?

Thanks in Advance.

Best Answer

There's a lookup on the Opportunity to pricebook called Pricbook2ID. Just query and check to see if it null.

From: https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_opportunity.htm





Create, Defaulted on create, Filter, Group, Nillable, Sort, Update


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.