TARANG GOEL

Salesforce CPQ Quote Calculation Sequence Part- 2

In the first part, I did some basics like the differences between the Price Waterfall and the Quote Calculation sequence, the definition of calculation sequence, and some basics on calculators. I would say that this part is meant for developers and CPQ technical architects but my endeavor to make it understandable to all players involved in CPQ implementation.

In this blog, we will look at the following:                          

  • JSQCP Methods and Plugins & Price Rules
  • Stages of Calculation
  • How does it treat Price Rules and JSQCP during Calculation?
  • Some other examples

JSQCP Methods and Plugins & Price Rules:

In order to understand the stages of calculation, it is especially important to understand what is Java Script Quote Calculation Plugin (JSQCP), the methods it offers, and Price Rules.

JSQCP– A plugin is a specific area to write code to introduce extra functionality. We know that Salesforce CPQ is a managed package and therefore to add flexibility, Salesforce has introduced the plugin so that Client and Org specific extra functionality can be introduced.

JSQCP is based on JavaScript and it is client-side coding. JSQCP marks a shift from apex-based coding which was server-side and hence relatively less efficient. JSQCP is used when an extra quote calculation functionality cannot be achieved by configuration (Price Rules).

JSQCP comes with some standard methods which are used to write and invoke code. These methods are On Initialization, On BeforeCalculate, Before Price Rules, After Price Rules. On After Calculate, Is field Visible. As the names suggest these are also events that happen during the calculation.

Price Rules- Price rules update quote line fields and automate quote calculations. They can be configured. They are configurable. Price rules do not require writing code. If you recall that in Part 1 I mentioned the legacy calculator which uses Process Builder and Workflow to update quote lines and fields. The same functionality can be achieved by Price rules using an advanced calculator and is recommended by Salesforce.

Examples

In Salesforce CPQ, A specific pricing can be achieved either via standard feature or configuration or code. Let us take one example here to understand

Example 1– Tango foods are offering Lunchbox at a list price of $15. There is a 10% system discount available for an order of more than 20 boxes. Now, this functionality can be achieved via discount schedules, via Price rules, and via code also writing a simple plugin. How this is accomplished depends on the context of the implementation.

Let us take other examples:

Example 2– Tango Foods is offering a 10% discount if the customer is new

Example 3–  Tango Foods does not want to offer an additional discount to the customer if the system discount is more than 5 percent. In this case, the additional discount field is locked

Example 4– Tango foods want to offer a 5% discount on the quote if the customer has purchased more than 100 lunchboxes in the last 6 months

Example 5– Tango foods have started offering Lunchbox as a monthly subscription and started prorating it. In that case, they want to know the exact subscription start and end date based on the subscription term.

Example 6– Tango foods want to display the total quote value by the group of products on the quote

Example 7– Tango foods want to offer a seasonal customer discount on some products

Now let us consider following scenarios:

  • The above examples are executed all by Price rules
  • Executed via JSQCP
  • Some executed by JSQCP and some by Price rules
  • Some of the above can be executed in a standard manner

Now the following considerations are especially important:

  • If multiple price rules are used, then in which order do they get executed
  • If multiple price rules are used, then when do they get executed during quote calculation and this is most important
  • If logic is being developed in JSQCP then when is that logic getting executed?
  • If multiple pricing functionalities are getting developed via JSQCP then in which order do they get executed and when during quote calculation à The questions
  • If functionality is being developed via both price rules and JSQCP, what is most important is in what order will the execution take place and when during quote calculation

In order to develop the above functionality, the knowledge of calculation sequence or calculation stages is most important. It is with this in mind that we will now describe the calculation stages and you will be able to put connect all the dots.

Stages of Calculation (Quote Calculation sequence)

First, let us understand what is quote calculation and when does it get triggered. For example, a Sales user of Tango Foods is creating a quote and adds the Lunchbox Product in the quote line, List price of $15 will be loaded and the calculation starts, here are some other scenarios when quote calculation is triggered.

  • Add a product to quote line
  • Products are deleted from the quote or quote lines are deleted
  • Calculate button on the quote line editor is clicked
  • When the save or quick save button on the quote line is clicked
  • When the following fields are populated or edited in the quote line
    • Additional discount
    • Distributor discount
    • End date
    • First segment term end date
    • Mark Up %
    • Partner discount
    • Start date
    • Subscription term
    • Other fields can be included to the group of the above-mentioned fields

Now let us see what happens behind the scenes when the quote calculation starts and these behind the scene actions are also called Stages of Quote Calculation.

Stage 1- Loading and Initialization- Products are loaded, bundles are loaded, options are loaded, Executes Price rules where the Event is Initialization, and JSQCP plugins are initialized. So for example if the total price of all components has to calculated and stored in a custom field on a quote and if this is solved via either Price rule or JSQCP the event or method will be On initialization and Onaftercalculate because this functionality is taking into account all components of the quote as soon as they are loaded or determined after a calculation. In such a scenario this development will be triggered in stage 1. CPQ implementation team and developers must know the calculation sequence to predetermine the triggering point of the code

Stage 2First round of calculations– Formulae fields are calculated on quote lines and quotes. Execute price rules where the value in the Event is Before Calculate. In the JSQCP OnBefore Plugin method is executed. So, any functionality developed with either the Before Calculate event or OnBeforeCalculate method is executed in the first round.

For example, Tango Foods is providing healthy snacks on every order created during COVID. Now, this can be executed with On Init and Before Calculate method or Oninit and After calculation method. Depending on the context as to where this needs to be accounted for and the knowledge of the execution timing, this will be developed. If this must be executed early and before the calculation, then the onBeforeCalculate method will be used.

Stage3- Second round of Calculation- Let us consider Example No 4, Tango Foods wants to offer a 5% additional discount to orders where the customers have purchased more than 100 lunchboxes in the last 6 months. This can be developed via a price rule or a JSQCP with an OnCalculationMethod or Event and this will be evaluated during stage 3.

Stage 3 of calculation executed the plugin with OnBeforPriceRules Method then On Calculate event on Price rules and then the OnAfterPrice rules Method with Plugin. It resets the target discounts if the quote contains a target amount. So, Let’s take another example, if a Plugin is written to Insert records (quote lines) before the price rules are evaluated, it can be done with the onBeforePricerules Method, and then price rules can be executed with OnCalculate event to calculate the totals. The development team needs to understand that these 2 pieces of code will be evaluated at this stage.

Stage4Third round of Calculation– Calculates fixed and dynamic items on the quote. Execute price rules where the Event OnCalculate and plugin where the method is onAfterCalculate.

Stage5Final Evaluation and Update– Evaluates formulae fields, MDQ segments, and deletes plugin modules.

https://sforce.co/36yPcD6I have not mentioned all the details of the quote calculation stages because they can be accessed from this link https://sforce.co/36yPcD6. Idea is to explain the importance of calculation stages while doing CPQ development and enabling multiple pricing requirements through Price Rules and Plugin’s and to understand the interplay between Price Rules Plugin’s and their evaluation sequence governed through Quote Calculation Sequence.

I hope that I have been able to explain this concept to you. Your likes, events, and shares will keep me motivated.

About 

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *