/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
* Script Deployment : REALLOCATE ITEM
*
* This script summarises creating a custom log record for reallocate item transactions.
* 1. Getting current username using N/record module objects.
* 2. Getting all field values from reallocate item page and storing it in a variable
* 3. On submitting the record, a custom record is created with these field values
* 4. And this record is the new log record of the rellocate item page transactions
*/
/**************************************************************************
Revision 1.0 {2 July 2021}- Reallocation page enhancement
* *************************************************************************/
define([‘N/record’, ‘N/runtime’, ‘N/search’, ‘N/ui/serverWidget’],
/**
* @param{record} record,runtime
*/
(record, runtime, search, serverWidget) => {
const beforeLoad = (scriptContext) => {
/**************CREATING A HIDDEN FIELD************************/
var form = scriptContext.form;
var field = form.addField({id: ‘custpage_commit’, type: serverWidget.FieldType.TEXTAREA, label: ‘Commit’});
field.updateDisplayType({displayType: serverWidget.FieldDisplayType.HIDDEN});
}
const beforeSubmit = (scriptContext) => {
if (scriptContext.type !== scriptContext.UserEventType.CREATE)
return;
var Re_record = scriptContext.newRecord;
try {
var item = Re_record.getValue({fieldId: ‘item’});
var obj = {};
var count = Re_record.getLineCount({sublistId: ‘order’});
var Soarray = [];
for (let i = 0; i < count; i++) {
const checkbox = Re_record.getSublistValue({sublistId: ‘order’, fieldId: ‘commit’, line: i});
/***************SEARCHING FOR OLD COMMITED QUANTITIES**************************************/
if (checkbox == false) {
var comm = Re_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘quantitycommitted’,
line: i
});
if (comm === “”) {
var SO_id = Re_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘ordernumber’,
line: i
});
Soarray.push(SO_id);
}
}
if (checkbox == true) {
var SO_id = Re_record.getSublistValue({sublistId: ‘order’, fieldId: ‘ordernumber’, line: i});
Soarray.push(SO_id);
}
}
log.debug(“SOARRRAY”, Soarray);
var len = Soarray.length;
log.debug(“LENGTH”, len);
for (let j = 0; j < len; j++) {
var SOid = Soarray[j];
var SO_search = search.create({
type: ‘salesorder’,
filters: [[‘tranid’, search.Operator.IS, SOid], ‘and’, [‘mainline’, search.Operator.IS, ‘F’], ‘and’, [‘shipping’, search.Operator.IS, ‘F’], ‘and’, [‘taxline’, search.Operator.IS, ‘F’], ‘and’, [‘cogs’, search.Operator.IS, ‘F’], ‘and’, [‘item’, search.Operator.IS, item]],
columns: [{name: ‘quantitycommitted’, summary: ‘SUM’}, {name: ‘tranid’, summary: ‘GROUP’}]
});
var qty;
SO_search.run().each(function (result) {
qty = result.getValue({
name: ‘quantitycommitted’, summary: ‘SUM’
});
return true;
});
if (qty === “”) {
qty = 0;
}
Re_record.getField({fieldId: ‘custpage_commit’});
obj[SOid] = qty;
}
/********************OLD COMMITED QUANTIES ARE SET TO A OBJECT**********************************/
log.debug(‘OBJECTS’, obj);
Re_record.setText({fieldId: ‘custpage_commit’, text: JSON.stringify(obj), ignoreFieldChange: true});
} catch (e) {
log.debug({title: e.name, details: e.message});
log.error({title: e.name, details: e.name});
}
}
const afterSubmit = (scriptContext) => {
try {
if (scriptContext.type !== scriptContext.UserEventType.CREATE)
return;
//getting the current user name
var user = runtime.getCurrentUser();
var username = user.name;
var Reallocate_record = scriptContext.newRecord;
var obj3 = Reallocate_record.getValue({fieldId: ‘custpage_commit’});
var obj4 = JSON.parse(obj3);
log.debug(“OBJ3”, obj3);
var item = Reallocate_record.getValue({
fieldId: ‘item’
});
//getting location field value
var location = Reallocate_record.getValue({
fieldId: ‘location’
});
//getting sublist line count
var count = Reallocate_record.getLineCount({sublistId: ‘order’});
const ab = function CreateLog1() {
//iterating upto sublist count and grabbing each line values
for (var i = 0; i < count; i++) {
var checkbox = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘commit’,
line: i
});
if (checkbox == true) {
var SO_id = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘ordernumber’,
line: i
});
var SO_search = search.create({
type: ‘salesorder’,
filters: [[‘tranid’, ‘is’, SO_id], ‘and’, [‘mainline’, ‘is’, ‘F’], ‘and’, [‘shipping’, ‘is’, ‘F’], ‘and’, [‘taxline’, ‘is’, ‘F’], ‘and’, [‘cogs’, ‘is’, ‘F’], ‘and’, [‘item’, ‘is’, item]],
columns: [{name: ‘quantitycommitted’, summary: ‘SUM’}, {name: ‘item’, summary: ‘GROUP’}]
});
var newqty_committ = 0;
SO_search.run().each(function (result) {
newqty_committ = result.getValue({
name: ‘quantitycommitted’, summary: ‘SUM’
});
return true;
});
if (newqty_committ === “”) {
newqty_committ = 0;
}
//grabbing sublist values
var qty_committed = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘quantitycommitted’,
line: i
});
var qty_remaining = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘quantityremaining’,
line: i
});
var customer = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘customer’,
line: i
});
var order_number = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘ordernumber’,
line: i
});
var order_date = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘orderdate’,
line: i
});
var Log_record = record.create({
type: ‘customrecord_jj_cr_ri_history_mar_694’,
isDynamic: true
});
var oldqnty = 0;
for (var key in obj4) {
if (key === order_number) {
oldqnty = obj4[key];
log.debug(“OLDQTY”, oldqnty);
}
}
//setting the custom record field values
Log_record.setValue({fieldId: ‘custrecord_jj_item_mar_694’, value: item});
Log_record.setValue({fieldId: ‘custrecord_jj_qty_remain_mar_694’, value: qty_remaining});
Log_record.setValue({fieldId: ‘custrecord_jj_location_mar_694’, value: location});
Log_record.setValue({fieldId: ‘custrecord_jj_old_comm_qty_mar_723’, value: oldqnty});
Log_record.setValue({fieldId: ‘custrecord_jj_customer_mar_694’, value: customer});
//Check whether the document number prefix is SO
if (order_number[0] === “S” && order_number[1] === “O”) {
Log_record.setText({
fieldId: ‘custrecord_jj_order_number_mar_694’,
text: ‘Sales Order #’ + order_number,
ignoreFieldChange: true
});
}
//Check whether the document number prefix is TO
if (order_number[0] === “T” && order_number[1] === “O”) {
Log_record.setText({
fieldId: ‘custrecord_jj_order_number_mar_694’,
text: ‘Transfer Order #’ + order_number,
ignoreFieldChange: true
});
}
Log_record.setValue({fieldId: ‘custrecord_jj_order_date_mar_694’, value: order_date});
Log_record.setValue({fieldId: ‘custrecord_jj_user_mar_694’, value: username});
Log_record.setValue({fieldId: ‘custrecord_jj_qty_committed_mar_694’, value: qty_committed});
Log_record.setValue({fieldId: ‘custrecord_jj_new_qty_mar_723’, value: newqty_committ});
//Try catch to grab exceptions
try {
//saving the custom record
Log_record.save({ignoreMandatoryFields: true});
} catch (e) {
log.error({
title: e.name, details: e.message
});
}
}
}
};
const ac = function CreateLog2() {
for (let i = 0; i < count; i++) {
const checkbox = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘commit’,
line: i
});
if (checkbox == false) {
var comm = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘quantitycommitted’,
line: i
});
if (comm === “”) {
var SO_id = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘ordernumber’,
line: i
});
var SO_search = search.create({
type: ‘salesorder’,
filters: [[‘tranid’, ‘is’, SO_id], ‘and’, [‘mainline’, ‘is’, ‘F’], ‘and’, [‘shipping’, ‘is’, ‘F’], ‘and’, [‘taxline’, ‘is’, ‘F’], ‘and’, [‘cogs’, ‘is’, ‘F’], ‘and’, [‘item’, ‘is’, item]],
columns: [{name: ‘quantitycommitted’, summary: ‘SUM’}, {
name: ‘item’,
summary: ‘GROUP’
}]
});
var qty_committ = 0;
SO_search.run().each(function (result) {
qty_committ = result.getValue({
name: ‘quantitycommitted’, summary: ‘SUM’
});
return true;
});
if (qty_committ === “”) {
qty_committ = 0;
}
log.debug(“NEWQTY”, qty_committ);
var qty_remaining = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘quantityremaining’,
line: i
});
var qty_committed = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘quantitycommitted’,
line: i
});
if (qty_committed === “”) {
qty_committed = 0;
}
var customer = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘customer’,
line: i
});
var order_number = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘ordernumber’,
line: i
});
var order_date = Reallocate_record.getSublistValue({
sublistId: ‘order’,
fieldId: ‘orderdate’,
line: i
});
var Log_record = record.create({
type: ‘customrecord_jj_cr_ri_history_mar_694’,
isDynamic: true
});
var oldqty = 0;
for (var key in obj4) {
if (key === SO_id) {
oldqty = obj4[key];
log.debug(“OLDQTY”, oldqty);
}
}
//setting the custom record field values
Log_record.setValue({fieldId: ‘custrecord_jj_item_mar_694’, value: item});
Log_record.setValue({
fieldId: ‘custrecord_jj_qty_remain_mar_694’,
value: qty_remaining
});
Log_record.setValue({fieldId: ‘custrecord_jj_location_mar_694’, value: location});
Log_record.setValue({fieldId: ‘custrecord_jj_old_comm_qty_mar_723’, value: oldqty});
Log_record.setValue({fieldId: ‘custrecord_jj_customer_mar_694’, value: customer});
//Check whether the document number prefix is SO
if (order_number[0] === “S” && order_number[1] === “O”) {
Log_record.setText({
fieldId: ‘custrecord_jj_order_number_mar_694’,
text: ‘Sales Order #’ + order_number,
ignoreFieldChange: true
});
}
//Check whether the document number prefix is TO
if (order_number[0] === “T” && order_number[1] === “O”) {
Log_record.setText({
fieldId: ‘custrecord_jj_order_number_mar_694’,
text: ‘Transfer Order #’ + order_number,
ignoreFieldChange: true
});
}
Log_record.setValue({fieldId: ‘custrecord_jj_order_date_mar_694’, value: order_date});
Log_record.setValue({fieldId: ‘custrecord_jj_user_mar_694’, value: username});
Log_record.setValue({fieldId: ‘custrecord_jj_new_qty_mar_723’, value: qty_committ});
Log_record.setValue({
fieldId: ‘custrecord_jj_qty_committed_mar_694’,
value: qty_committed
});
//Try catch to grab exceptions
try {
//saving the custom record
Log_record.save({ignoreMandatoryFields: true});
} catch (e) {
log.error({
title: e.name, details: e.message
});
}
}
}
}
}
ab();
ac();
} catch (e) {
log.error({title: e.name, details: e.message});
}
}
return {beforeLoad, beforeSubmit, afterSubmit}
});