How can I update prices on product lines?

You can update a price for a product line by using a groovy script to pull the list price for a product from the selected price book and stamp that value in the Amount field on the product line.

Here are the steps.

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

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

  3. Select the Triggers tab.

  4. In the Server Scripts Opportunity Revenue page, in the Field Triggers section, select the Add a New Trigger icon. The page changes to the Create Field Trigger page, and the Trigger Type field defaults to After Field changed.

  5. In the Create Field Trigger page, select Product for the Field Name.

    Note: If there are two instances of Product in the list, select the first one.
  6. In the Trigger Name field, enter FetchPrice.

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

    //Get the ID of the item:
    def var_InventoryItemId = nvl(getAttribute('InventoryItemId'), (long) 0) 
    //Get the ID of the price book (this is useful if you have multiple price books):
    def var_DCL_PriceBook = Opportunity?.PriceBook_Id_c // 
    //PriceBook is a dynamic choice list on the opportunity.
    //Make a view of the PriceBookItem object, since that's where the prices for items are stored:
    def view_PriceBookItem = newView('PriceBookItem')
    def view_Criteria = newViewCriteria(view_PriceBookItem)
    def view_criteria_row = view_Criteria.createRow()
    
    //Define the first sort criteria. Basically, find all records in PriceBookItem that match the ID of the InventoryItemId on revenue.
    def view_condition = view_criteria_row.ensureCriteriaItem('InvItemId')
    view_condition.setOperator('=')
    view_condition.setValue(var_InventoryItemId)
    
    //Define the second sort criteria. If there are multiple price books that contain the same item with different prices, get the price of the item corresponding to the price book selected in the dynamic choice list on the opportunity.
    def view_condition_1 = view_criteria_row.ensureCriteriaItem('PricebookId')
    view_condition_1.setOperator('=')
    view_condition_1.setValue(var_DCL_PriceBook)
    
    //Execute the sort and query the records:
    view_Criteria.insertRow(view_criteria_row)
    view_PriceBookItem.appendViewCriteria(view_Criteria)
    view_PriceBookItem.setMaxFetchSize(1400) //1,400 is the number of records in the system; change this number according to your system records.
    view_PriceBookItem.executeQuery()
    
    def var_price // This is a variable that will hold the price from PriceBookItem
    while(view_PriceBookItem.hasNext()){
    def var_Row = view_PriceBookItem.next()
    
    //Get the price from PriceBookItem for the item you want:
    var_price = var_Row.getAttribute('ListPrice') 
    }
    //Assign the price to the revenue field UnitPrice:
    setAttribute('UnitPrice',var_price) 
  8. Click Save and Close.

  9. Verify that the FieldName in the Triggers section appears as InventoryItemId.
  10. Test the changes and publish the sandbox.