Jira Code: TG-43
Need to populate the preferred bin and quantity available to the picking ticket PDF. So first need to populate it to sales order item line from the item record. Then to the Picking ticket.
User Event script
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
/*******************************************************************************
* CLIENTNAME:TELEGRAM
* TG-43 : To add Prefered bin and qty available in Picking ticket, to add these fields in SO
*************************************************************************
* Date : 07/05/2019
*
* Author: Jobin & Jismi IT Services LLP
* Script Description : To achieve the task, we have to add bin and qty available in SO
* Date created : 07/05/2019
*
* REVISION HISTORY
*
* Revision 1.0 ${07/05/2019} aj : created
*
*
******************************************************************************/
define([ 'N/record', 'N/search' ],
/**
* @param {record} record
* @param {search} 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 {
// to get the id
var soId = scriptContext.newRecord.id;
// to get the each line num
var soRec = record.load({
type : 'salesorder',
id : soId,
isDynamic : true
});
// location
var location = soRec.getValue({
fieldId : 'location'
})
// to get the line num
var lineNum = soRec.getLineCount({
sublistId : 'item'
});
for (var i = 0; i < lineNum; i++) {
// to get each item & its type
var item = soRec.getSublistValue({
sublistId : 'item',
fieldId : 'item',
line : i
});
// to get the bin Number & Available
var binObj = getBinAndAvailable(item, location);
// to set the available qty & Bin
if (Object.keys(binObj).length > 0) {
// inn
// to swelewct linr
var lineNum = soRec.selectLine({
sublistId : 'item',
line : i
});
soRec.setCurrentSublistText({
sublistId: 'item',
fieldId:'custcolbinnumbercust',
text:binObj.bin,
ignoreFieldChange: true
});
soRec.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_qty_available_custom',
value: binObj.available,
ignoreFieldChange: true
});
// to commit
soRec.commitLine({
sublistId: 'item'
});
}
}
var soID=soRec.save();
} catch (e) {
log.debug("Err@ afterSubmit FN ", e);
log.error("Err@ afterSubmit FN ", e);
}
}
/****
* Function to get the bin number & available
*/
function getBinAndAvailable(itemNumber, location) {
try {
//creating search
var itemSearchObj = search.create({
type : "item",
filters : [ [ "internalidnumber", "equalto", itemNumber ],
"AND", [ "preferredbin", "is", "T" ], "AND",
[ "binnumber.location", "anyof", location ] ],
columns : [ search.createColumn({
name : "itemid",
sort : search.Sort.ASC,
label : "Name"
}), search.createColumn({
name : "binnumber",
label : "Bin Number"
}), search.createColumn({
name : "preferredbin",
label : "Preferred Bin"
}), search.createColumn({
name : "binonhandavail",
label : "Bin On Hand Available"
}) ]
});
var searchResultCount = itemSearchObj.runPaged().count;
var retnObj = {};
if (searchResultCount > 0) {
var searchResult = itemSearchObj.run().getRange({
start : 0,
end : 1
});
// to get the SO#
retnObj.bin = searchResult[0].getValue({
name : "binnumber",
label : "Bin Number"
});
retnObj.available = searchResult[0].getValue({
name : "binonhandavail",
label : "Bin On Hand Available"
});
}
return retnObj;
} catch (e) {
log.debug("Err@ getBinAndAvailable FN ", e);
log.error("Err@ getBinAndAvailable FN ", e);
}
}
return {
afterSubmit : afterSubmit
};
});