Introduction
ATTENTION : The code is not the optimal, we have too many problem for this, need more time
Custom script
// Exported function that is called after CPQ calculates the quote.
export async function onAfterCalculate(quoteModel, quoteLineModels, conn) {
// Apply custom discount logic after calculation
setDiscounts(quoteModel, quoteLineModels, conn);
// Return a resolved promise indicating completion
return Promise.resolve();
}
// Function to set custom discounts based on the quote lines.
async function setDiscounts(_quoteModel, quoteLineModels, conn) {
// Check if there are any quote line items
if (quoteLineModels.length > 0) {
let dts = []; // Array to store discount tiers
// Query discount tiers from Salesforce
return conn.query('SELECT Id, SBQQ__Schedule__r.SBQQ__Product__r.Name, SBQQ__Schedule__r.SBQQ__Product__r.Family, SBQQ__Schedule__c, SBQQ__LowerBound__c, SBQQ__UpperBound__c, SBQQ__DiscountAmount__c FROM SBQQ__DiscountTier__c')
.then(function (discountTiers) {
// Check if discount tiers are available
if (discountTiers.totalSize) {
console.log("in discount tier, total size :" + discountTiers.totalSize)
dts = discountTiers;
}
// Query discount schedules from Salesforce
return conn.query('SELECT Id, Name, SBQQ__Product__c, SBQQ__Product__r.Family, SBQQ__Product__r.Name, SBQQ__Product__r.ProductCode FROM SBQQ__DiscountSchedule__c');
}).then(function (discountSchedules) {
// Check if discount schedules are available
if (discountSchedules.totalSize) {
// Iterate over each discount schedule record
console.log("in discount schedule, total size :" + discountSchedules.totalSize)
discountSchedules.records.forEach(function (discountScheduleRecord) {
// Iterate over each quote line
console.log("discountScheduleRecord.Name " + discountScheduleRecord.Name)
quoteLineModels.forEach(function (line) {
line.record['RequiredByProductCode'] = null
console.log("before assignment " + line.record['RequiredByProductCode'])
// Check and assign the RequiredByProductCode if available
if (line.record['SBQQ__RequiredBy__r']) {
line.record['RequiredByProductCode'] = line.record['SBQQ__RequiredBy__r']['SBQQ__ProductCode__c'];
console.log("after assignment " + line.record['RequiredByProductCode'])
}
console.log("line.record['SBQQ__ProductName__c'] " + line.record['SBQQ__ProductName__c'])
// Check for matching product names
if (discountScheduleRecord.Name == line.record['SBQQ__ProductName__c']) {
console.log("in the if for discountScheduleRecord.Name == line.record['SBQQ__ProductName__c']")
// Apply discount logic for installation lines not matching specific criteria
console.log(line.record['SBQQ__ProductFamily__c'])
console.log(line.record['SBQQ__ProductCode__c'])
console.log(line.record['RequiredByProductCode'])
if (line.record['SBQQ__ProductFamily__c'] == 'Test' && !(line.record['SBQQ__ProductCode__c'] == 'DBPQ1' && line.record['RequiredByProductCode'] == 'DPBTQ1')) {
console.log("in the if for line.record['SBQQ__ProductFamily__c'] == 'Test' && !(line.record['SBQQ__ProductCode__c'] == 'DBPQ1' && line.record['RequiredByProductCode'] == 'DPBTQ1')")
// Iterate over discount tiers
dts.records.forEach(function (dt) {
console.log("dts.record " + dt.Name)
// Apply discount if conditions are met
if (dt.SBQQ__Schedule__c == discountScheduleRecord['Id'] && line.record['SBQQ__Quantity__c'] > 1) {
console.dir('entering entering calc');
// Set special price description, type, and calculate the special price
line.record["SBQQ__SpecialPriceDescription__c"] = 'Volume discount applied';
line.record["SBQQ__SpecialPriceType__c"] = "Custom";
console.log('For QuoteLine Number: ' + line.record['SBQQ__Number__c']);
console.log('assigning: ' + (line.record.SBQQ__ListPrice__c - dt.SBQQ__DiscountAmount__c) + ' to Special Price');
line.record["SBQQ__SpecialPrice__c"] = (line.record.SBQQ__ListPrice__c - dt.SBQQ__DiscountAmount__c);
console.log('resultant netprice: ' + line.record["SBQQ__SpecialPrice__c"]);
}
});
}
}
})
})
}
})
}
}
Conclusion
We need to improve the target. For example, they take all discount tiers and discount schedules. However, we need specific value. What’s more, they take in consideration we have only one discount tier. We need to update this to have for all.
Result
the link for the github: https://github.com/AourLegacy/AourFactory/tree/Discount-tiers
Comments