Objective:
In this blog, we will learn how to roll up the child record values from the parent bundle in Salesforce CPQ using the Quote Calculator Plugin (QCP).
Scenario:
We have a bundle called “Enterprise” which includes several Software, Payment and One Time child products. We need to:
- Calculate the total net price of all software products within the current bundle.
- Add 10% of this total amount to the setup fee list price.
- Ensure that the setup fee calculation is applied only once per unique bundle, even if the same bundle is added multiple times.
Note: It is possible for the same bundle to be added multiple times. The setup fee calculation should be applied only once per unique bundle, ensuring the price is calculated correctly for each instance. and That’s why we are using QCP to calculate it
Steps to Achieve the Objective
Step 1: Define the QCP Code
Below is the QCP code that calculates the total net price of all software child products and updates the setup fee list price accordingly.
export function onAfterPriceRules(quote, lines, conn) {
var bundleNetTotalMap = new Map();
lines.forEach(function (line) {
if(line.record.SBQQ__ProductFamily__c == 'Software' && line.parentItem != '' && line.parentItem != null ){
if(bundleNetTotalMap.has(line.parentItem.Id)){
bundleNetTotalMap.set(line.parentItem.Id,bundleNetTotalMap.get(line.parentItem.Id)+line.record.SBQQ__NetTotal__c);
}else{
bundleNetTotalMap.set(line.parentItem.Id,line.record.SBQQ__NetTotal__c);
}
}
});
lines.forEach(function (line) {
if(line.record.SBQQ__ProductCode__c == 'PO-SF' && line.parentItem.Id != '' && line.parentItem.Id != null && line.parentItem != undefined && bundleNetTotalMap.size >0 && bundleNetTotalMap.has(line.parentItem.Id)) {
line.record.SBQQ__ListPrice__c = bundleNetTotalMap.get(line.parentItem.Id) * 0.10;
}
});
return Promise.resolve();
}
Also Check This – How to Send QR Code through Email in Salesforce
Explanation of the Code:
- Initialize
netTotal
Map: We initialize abundleNetTotalMap
Map to store the total net price of software products for each parent bundle. - First Loop – Calculate Total Net Price:
- We loop through all the quote lines.
- Check if the line item is part of the ‘Software’ product family and has a valid parent item.
- If the parent item is already in the
bundleNetTotalMap
map, add the current line’s net total to the existing total. - If the parent item is not in the map, create a new entry with the current line’s net total.
- Second Loop – Update Setup Fee List Price:
- We loop through the quote lines again.
- Check if the line item’s product code is ‘PO-SF’, has a valid parent item, and if the netTotal map has a corresponding entry for the parent item.
- If these conditions are met, set the line item’s list price to 10% of the parent’s net total.
Why We Used onAfterPriceRules
We used onAfterPriceRules
method because:
- Timing: The
onAfterPriceRules
callback is executed after the standard pricing rules have been applied. This ensures that any custom calculations we perform do not interfere with or get overwritten by the standard CPQ pricing logic. - Access to Updated Values: By this stage, all pricing-related fields (like net total) have been calculated and are available for our custom logic.
- Customization: It provides the flexibility to customize the pricing logic further, ensuring our setup fee calculation is accurately applied based on the total net price of the software products.
By following these steps, we ensure that the setup fee is calculated correctly and applied to each unique instance of the bundle, adhering to the specified requirements.
These are the child products we are having in our Enterprise bundle.
Product Name | Product Code | Product Family |
Webchat | PS-WEB | Software |
Messaging | PS-MSG | Software |
Analytics Dashboard | PS-ADASH | Software |
Social Media Integration | PS-SMI | Software |
Payments | PP-Pay | Payment |
Setup Fee | PO-SF | One-Time |
In the below screen we will see how it is calculating the setup fee list price for this scenario
For a comprehensive guide on all the JavaScript Quote Calculator Plugin methods, refer to this link.