Set Default Price Book and Get Prices

You can use a Groovy script to set the default price book on an opportunity and get the latest prices onto the price book. The default price book can be set based on various opportunity attributes.

These opportunity attributes can be, for example, currency, customer geography, or customer account.

Here's how to set a default account price book on an opportunity.

Note that this procedure also sets a default Corporate price book on an opportunity, if an account price book can't be found.

  1. Ensure you're working in an active sandbox.

  2. In Application Composer, navigate to Standard Objects > Opportunity > Server Scripts.

  3. Select the Triggers tab.

  4. Select the Add a New Trigger icon.

  5. In the Trigger field, select Before Insert in Database.

  6. In the Trigger Name field, enter SetPriceBook.

  7. In the Trigger Definition region, enter this script:

    //Fetch the Id of Account Pricebook
    def pricebookId = Organization?.PriceBook_Obj_c?.PricebookId;
    
    // Set the Id of Account PriceBook to Opty
      setAttribute('PriceBook_Id_c', pricebookId);
  8. Click Save and Close.

  9. Test the changes and publish the sandbox.

If a price book isn't defined in the Account object, you can define a default price book in Groovy as follows:
def accountplist=Organization?.getAttribute('PriceBook_c')
def optyplist=getAttribute('PriceBook_c')
if (optyplist == null ) {
   if (accountplist == null ){
      optyplist = 'Corporate' //This is the name of the default pricebook
   }
  setAttribute('PriceBook_c',accountplist)
Note: You can also add the Default Price Book check box to the Edit Price Book page using Application Composer > Price Book Header object > Details Page Layouts.