Jira Code: BG-10
In a sales order when selecting a particular segment (from the dropdown list of the field ” Main Product segment”) in the line level. Then the script will populate a value to another field called ” Segment Budget” in line level of that same item, from a custom record that created (Product category budget). The value should be fetched based on some criteria from the custom record. which is a year, month, sales rep name, segment value.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
/* Script Description
* Auto update field value in SO from corresponding Product Category Budget record.
******************************************************************************************************
Salesman Performance Report
******************************************************************************************************
* Date: 12/04/2019
*
* Author: Jobin & Jismi IT Services LLP*/
/*****************************************************************************************************/
define(['N/record', 'N/search'],
function(record, search) {
/**
* Function definition to be triggered before record is loaded.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {string} scriptContext.type - Trigger type
* @param {Form} scriptContext.form - Current form
* @Since 2015.2
*/
/*function beforeLoad(scriptContext) {
}*/
/**
* Function definition to be triggered before record is loaded.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type
* @Since 2015.2
*/
/*function beforeSubmit(scriptContext) {
}*/
/**
* Function definition to be triggered before record is loaded.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type
* @Since 2015.2
*/
function afterSubmit(scriptContext) {
try {
var Months = {
1: "custrecord_jj_bg_2_january",
2: "custrecord_jj_bg_feb",
3: "custrecord_jj_bg_2_march",
4: "custrecord_jj_bg_2_april",
5: "custrecord_jj_bg_2_may",
6: "custrecord_jj_bg_2_june",
7: "custrecord_jj_bg_2_july",
8: "custrecord_jj_bg_2_august",
9: "custrecord_jj_bg_2_september",
10: "custrecord_jj_bg_2_october",
11: "custrecord_jj_bg_2_november",
12: "custrecord_jj_bg_2_december"
};
var REC = scriptContext.newRecord.id;
var SALES_ORDER = record.load({
type: record.Type.SALES_ORDER,
id: REC,
isDynamic: false
});
var sales_RepVal = SALES_ORDER.getValue({
fieldId: 'salesrep'
});
var sales_Rep = SALES_ORDER.getText({ fieldId: 'salesrep' });
var trandate = SALES_ORDER.getValue({ fieldId: 'trandate' });
var tranYear = trandate.getFullYear();
var tranMonth = trandate.getMonth() + 1;
var monthName = Months[tranMonth];
var lineCountItemSublist = SALES_ORDER.getLineCount({ sublistId: 'item' });
for (var i = 0; i < lineCountItemSublist; i++) {
var main_Product_Category = SALES_ORDER.getSublistValue({
sublistId: 'item',
fieldId: 'cseg_jj_bg_2_pdtcat',
line: i
});
var custReccVal = LoadCustRec(sales_RepVal, tranYear, main_Product_Category, monthName);
SALES_ORDER.setSublistValue({
sublistId: 'item',
fieldId: 'custcol_jj_test_budget_prod_t',
value: custReccVal || 0.00,
line: i,
// ignoreFieldChange: true
});
// SALES_ORDER.commitLine({
// sublistId :'item'
// });
var CSRecordId = SALES_ORDER.save({
enableSourcing: 'false',
ignoreMandatoryFields: 'false'
});
log.debug("SUCCSESS",CSRecordId);
}
} catch (e) {
log.debug({
title: 'Mainfunction error',
details: e.message
});
}
}
/**********************************************************************************************************/
/* CEARTING SAVED SEARCH */
function LoadCustRec(sales_RepVal, tranYear, main_Product_Category, monthName) {
try {
var resultval;
var customrecord_jj_bg_2_product_segment_budSearchObj = search.create({
type: "customrecord_jj_bg_2_product_segment_bud",
filters: [
["custrecord_jj_bg_2_sales_rep", "anyof", sales_RepVal],
"AND",
["cseg_jj_bg_2_pdtcat", "anyof", main_Product_Category],
"AND",
["formulatext: {custrecord_jj_bg_2_year}", "is", ("" + tranYear)]
],
columns: [
search.createColumn({ name: monthName, label: monthName })
]
}).run().each(function(result) {
resultval = result.getValue(monthName);
return false;
});
return resultval ? resultval : false;
} catch (e) {
log.debug({
title: 'LoadCustRec error',
details: e.message
});
}
}
return {
/*beforeLoad: beforeLoad,
beforeSubmit: beforeSubmit,*/
afterSubmit: afterSubmit,
LoadCustRec: LoadCustRec
};
});