Jira Code: MT-38
The following is a workflow action script. This runs on the save of a sales order and adds discount percentage for different item categories. Items differ in the type, Pallets and Cosmetics.
A workflow runs whenever a sales order is saved. The following script is an action of workflow and helps in calculating the discounts.
MT-38 ML Partner Discount Workflow Action Script
/**
* @NApiVersion 2.x
* @NScriptType workflowactionscript
*/
/*******************************************************************************
* Merchandize Liquidators
* MT-38
******************************************************************************
* Date:
* Author: Jobin & Jismi IT Services LLP
* Script Description:
* Date created : 11-03-2019
define(['N/record', 'N/runtime', 'N/search'],
function(record, runtime, search) {
/**
* Definition of the Suitelet script trigger point.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @Since 2016.1
*/
function onAction(scriptContext) {
try {
var total_cosmetics = 0; //total amount of cosmetics items
var total_pallets = 0; //total amount of pallet items
var item_number = 0; //number of items in pallet category
var objrecord = scriptContext.newRecord;
log.debug("objrecord", objrecord);
var numLines = objrecord.getLineCount({
sublistId: 'item'
});
log.debug("Number of Line items", numLines);
log.debug('Line Index', numLines);
for (i = 0; i < numLines; i++) {
var sublistFieldValue = objrecord.getSublistValue({
sublistId: 'item',
fieldId: 'item',
line: i
});
var itemtype = objrecord.getSublistValue({
sublistId: 'item',
fieldId: 'itemtype',
line: i
});
if (itemtype == 'InvtPart') { //inventory items considered
var discount_category = search.lookupFields({
type: search.Type.INVENTORY_ITEM,
id: sublistFieldValue,
columns: ['custitem_mt_35_discount_rule']
});
log.debug('discount_category', discount_category);
if (discount_category.custitem_mt_35_discount_rule ==
'' || discount_category.custitem_mt_35_discount_rule ==
null || discount_category.custitem_mt_35_discount_rule == undefined) {
continue;
}
discount_category_value = discount_category.custitem_mt_35_discount_rule[0].value;
log.debug('discount_category_value', discount_category_value);
var item_amount = objrecord.getSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: i
});
log.debug('item_amount', item_amount);
var item_quantity = objrecord.getSublistValue({
sublistId: 'item',
fieldId: 'quantity',
line: i
});
log.debug('item_quantity', item_quantity);
if (discount_category_value == '1') {
total_cosmetics = total_cosmetics + item_amount;
log.debug('total_cosmetics', total_cosmetics);
} else if (discount_category_value == '2') {
total_pallets = total_pallets + item_amount;
item_number = item_number + item_quantity;
log.debug('total_pallets', total_pallets);
log.debug('item_number', item_number);
} else {
log.debug('Sorry');;
}
}
}
var total_cosmetics_discount = 0;
if ((total_cosmetics >= 10000) && (total_cosmetics <= 24999)) {
// 5 percent discount
total_cosmetics_discount = total_cosmetics * (1 / 20);
log.debug('total_cosmetics_discount', total_cosmetics_discount);
} else if (total_cosmetics >= 25000) {
// 10 percent discount
total_cosmetics_discount = total_cosmetics * (1 / 10);
log.debug('total_cosmetics_discount', total_cosmetics_discount);
} else {
log.debug('No Discount for Cosemtics');
log.debug('total_cosmetics_discount', total_cosmetics_discount);
}
if (total_cosmetics_discount != 0) {
var lineNum = objrecord.selectNewLine({
sublistId: 'item'
});
objrecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: -6
});
total_cosmetics_discount_negative = total_cosmetics_discount * -1;
log.debug('total_cosmetics_discount_negative', total_cosmetics_discount_negative);
objrecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: total_cosmetics_discount_negative
});
objrecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'description',
value: "Discount via webstore"
});
objrecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'istaxable',
value: true
});
log.debug('success 1');
objrecord.commitLine({
sublistId: 'item'
});
}
var total_pallets_discount = 0;
if ((item_number >= 4) && (item_number <= 9)) {
total_pallets_discount = total_pallets * (1 / 20);
} else if ((item_number >= 10) && (item_number <= 19)) {
total_pallets_discount = total_pallets * (1 / 10);
} else if ((item_number >= 20) && (item_number <= 39)) {
total_pallets_discount = total_pallets * (3 / 20);
} else if (item_number >= 40) {
total_pallets_discount = total_pallets * (1 / 5);
} else {
log.debug('No Discount for Pallets');
log.debug('total_pallets_discount', total_pallets_discount);
}
if (total_pallets_discount != 0) {
var lineNum = objrecord.selectNewLine({
sublistId: 'item'
});
objrecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: -6
});
total_pallets_discount = total_pallets_discount * -1;
log.debug('total_pallets_discount', total_pallets_discount);
objrecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'rate',
value: total_pallets_discount
});
objrecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'description',
value: "Discount via webstore"
});
objrecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'istaxable',
value: true
});
log.debug('success 2');
objrecord.commitLine({
sublistId: 'item'
});
}
} catch (er) {
log.debug('er', er);
}
}
return {
onAction: onAction
};
});