Skip to main content

Create Pricing Hooks

Learning Objectives

After completing this unit, you’ll be able to:

  • Discuss the purpose of pricing hooks.
  • Explain how pricing hooks work with Industries CPQ interfaces.
  • Describe how to set up pricing hooks in Industries EPC.

Price by Location

Eliza, our Infiwave developer, is working diligently through a list of requests for enhanced pricing capabilities in EPC and comes across a new challenge. This time, the marketing team wants to price products in the Industries CPQ Cart based on a customer’s location. This will help them provide attractive pricing for certain markets and increase prices in locations where it’s more costly to fulfill orders.

Being the star developer that she is, Eliza could make manual adjustments to the underlying programming code to set up location-based pricing. However, she knows these adjustments would require maintenance over time and overcomplicate the application. It’s best to keep things simple. Thank goodness for pricing hooks.

Pricing Hooks

EPC allows you to write hooks to implement custom logic without modifying the original behavior of the application or changing its underlying code. Hooks intercept commands so you can change an action that the application normally performs.

Hooks have a wide range of applications. From a pricing standpoint, hooks let you:

  • Add new pricing functionality.
  • Expose additional fields in pricing calculations.
  • Alter the pricing of products.

In Industries CPQ, a global interface called CpqAppHandler manages functionality and includes several methods that perform CPQ processes in the Cart. You can change the default behavior of these processes before the actual interface executes with a pre-invoke hook or change the behavior after execution with a post-invoke hook. To do this, you create a custom implementation called CpqAppHandlerHook in the hook interface of the CpqAppHandler.

In this diagram, the pre-invoke and post-invoke hooks insert custom processing before and after default CPQ processes.

CpqAppHandlerHook custom processes executing before and after CpqAppHandler processes.

Pre-invoke processing executes before rules, pricing, validation, and processing, and post-invoke processing runs after these default processes.

Use the CpqAppHandlerHook to manage inputs and outputs of CPQ methods.

  • Add, change, or delete input parameters.
  • Add, change, or delete output responses.
  • Debug issues by verifying inputs and outputs.
  • In some cases, provide a workaround for an issue while waiting for a product fix.

In addition, you can add business logic such as:

  • Implement conditional processing.
  • Preload information that is used further down the execution chain.
  • Process custom fields.
  • Implement custom functionality.
  • Do callouts.

The CpqAppHandlerHook is automatically invoked when the InvokeService is called. The InvokeService simply appends a hook string, for example, “Hook”, to the interface name and determines if there’s an active interface with that name. If so, the InvokeService initiates the active implementation in the hook interface.

Then, the service adds a .preInvoke suffix to the method name and invokes the PreInvoke method in the hook implementation. A corresponding .postInvoke method is invoked on the hook implementation after the hooked class method has completed its processing.

You can also use this implementation to perform conditional processing, pre-load information, process custom fields, and more. To learn more about how pricing hooks work behind the scenes, refer to the links in the Resources section.

To illustrate how hooks work, consider how Eliza can change Cart price calculation parameters. Currently, if a customer adds a phone product to the Cart, CPQ multiplies the selected quantity of the product by its one-time charge amount. But what if the customer wants to also see the sales tax applied to the total? Eliza can insert a pricing hook to multiply the charge amount and quantity by the applicable sales tax rate. She can even modify the Cart to display a custom column to show this sales tax calculation.

The ability to inject custom code is powerful when executed correctly, but you shouldn’t use hooks in every situation. Here are the recommendations for implementing a hook.

  • Validate that a feature doesn’t already exist that addresses your need for a hook.
  • Ensure the custom code in the hook doesn’t cause the execution to exceed any Apex governor limits.
  • Put a try-catch around the hook code to catch any exceptions thrown by the custom code.
  • When unit testing the hook code, it may be possible to mock the inputs expected by the hook code to minimize test setup and get better code coverage. Use Test.isRunningTest() to extract the mock input from the inputMap or a static map.

Follow these guidelines to ensure you’re not creating hooks you don’t need.

Price Hook Configuration for Pricing by Zip Code

Eliza is ready to address the marketing team’s request, and decides to use a hook to modify the price of a headphones product based on the customer’s zip code, also known as a postal code. Join Eliza as she implements this by following the steps listed in the Create a Pricing Hook practice guide.

When the pricing hook is successfully created, Eliza tests it by launching a new order and adding the headphones product to the Cart.

New order showing updated price of Infiwave Headphones 3 as $800.

The price automatically adjusts to the amount that corresponds with the customer’s billing zip code range.

Next Up

Eliza’s thrilled to know she can insert custom functionality without having to make changes to the actual Industries CPQ application. You might even say she’s hooked! Follow Eliza in the next unit as she sets up pricing based on a customer’s usage of a product.

Resources

Share your Trailhead feedback over on Salesforce Help.

We'd love to hear about your experience with Trailhead - you can now access the new feedback form anytime from the Salesforce Help site.

Learn More Continue to Share Feedback