In Edit quote lines screen of Salesforce CQP, we need to add a custom field "Budget" and add a JS script to auto-calculate the additional discount % applicable on all the lines, so that the quote total is equal to the custom field "Budget" amount.

The additional discount % field must be editable to that user can still adjust the discount line by line.

enter image description here

Research done so far

For the budget field, we found that a custom field must be created on Salesforce CPQ Quote object and added to the appropriate field set used by CPQ package, so that it becomes visible on the CPQ quote edit line screen.

It is also understood that the calculation can be achieved by the Javascript quote calculator plugin.

But we are new to CPQ and documentation found so far is not clear enough to answer the questions below but maybe we are missing something.

Call a Custom Script in the Quote Calculator Plugin

Javascript Quote Calculator Plugin


  1. where do we write/save the script to perform the additional discount % calculations in the quote line table ?
  2. Any pointers to some documentation about how to go about writing such script ?
  3. What is the procedure to deploy CPQ custom JS script to higher environments (sandbox and production) ?

Any help is appreciated, thanks

2 Answers 2

  1. You need to create a Custom Script record with your QCP implementation and later go to the CPQ package settings and indicatr the Name of the script in the Calculator Plugin field in the configuration.

  2. In your second link to the documentation you can find pretty much everything, particularly in the Plugin Methods one, you need to implement the exported functions that apply for your use case by modifying the quoteModel or quoteLineModels that are passed into the method as arguments.

  3. Since the scripts are records from the Custom Script object you'll need to either create / edit your record manually in other environments or use mechanisms that ease data deployments such as SalesforceDX import commands

  • thanks, I didn't realise that the term "custom script" was also used for the custom object that stores the script. this is a bit confusing. does this mean that we need to use a separate editor to write the code and just copy/paste it in the "code" field of the record. then does the custom script record validates the script to check for syntax issues ?
    – Shamina
    Feb 3, 2020 at 7:27
  • 1
    There's no syntax check per se, however there's a transpiling process (using Babel I think) that will transform your Javascript code into a transpiled version, the transpiled version is the one the CPQ injects into the calculation process. If the code is not transpilable then you'll get an (asynchronous) error after saving the Custom Script. I suggest you use an editor with a linter extension to avoid losing time with common syntax errors and typos. Feb 3, 2020 at 7:55
  • @Shamina I am about to do the same. Any additionally tips or examples?
    – CodeMonger
    Nov 21, 2021 at 22:03
  • @CodeMonger sorry no additional tips, we didnt move ahead with the implementation finally
    – Shamina
    Nov 22, 2021 at 6:05
  • 1
    @CodeMonger although quite outdated the concepts on this repo of example are still valid github.com/paustint/qcp-examples Nov 22, 2021 at 8:08

SO I created a custom script called budget. Added the following code to it:

export function onBeforeCalculate(quoteModel,quoteLineModels){ let budget = quoteModel.record.Budget__c; let initialTotal = quoteModel.record.SBQQ__NetAmount__c; let lineDiscountPercent = ((initialTotal-budget)/initialTotal)*100; if(budget<initialTotal){ quoteLineModels.forEach(function (line){ line.record.AdditionalDiscountUnit__c = 'Percent'; line.record.SBQQ__Discount__c = lineDiscountPercent; }); } return Promise.resolve(); }

Also create field on quote object called budget of type number or currency your choice. Add the custom script name to the calculator plugin the usual steps Add the quote fields and quote line fields used in the code to the respective fields on the custom script record that we just created.

note the additional discount unit should be percent for this to

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .