When a customer orders a custom cable, denoted with a “-B” at the end of the item name/number (for example, “12D50125MOM3P-B”) we would like to be able to track the # of spools and the length of those spools that the customer would like the item made into with two new Transaction Line Fields, “# of Spools” and “Length of Spools”. This way we would know the total quantity ordered, as well as the # of spools and length of those spools that should make up that total quantity. I.e., quantity = 5000, # of spools = 5, Length of Spools = 1000. Then we know the total length the customer wants as well as how they want that total length broken down into spools. We need these fields to populate form the specific “-B” item on the sales order to the specific component item on the work order. We will be creating below fields in PRODUCTION with following id:
Field Name: ID ideal ID
Number Of Spools: custcol10 custcol_ctg_num_of_spools
Length Of Spools: custcol11 custcol_ctg_lengh_of_spools
SOLUTION
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
/************************************************************************************************
CTG - 63 Work Order spool field updation script.
*********************************************************************************************
*
* Author: Jobin & Jismi IT Services LLP
*
* Date Created : 25th-April-2022
*
* Description : Work Order spool field updation script.
*
* REVISION HISTORY
*
***********************************************************************************************/
define(['N/record'],
/**
* @param{record} record
*/
(record) => {
/**
* Defines the function definition that is executed after record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord - New record
* @param {Record} scriptContext.oldRecord - Old record
* @param {string} scriptContext.type - Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const afterSubmit = (scriptContext) => {
try {
if (scriptContext.type === 'create') {
let currentRecord = scriptContext.newRecord;
log.debug("currentRecord", currentRecord)
let workOrderId = currentRecord.id
log.debug("workOrderId", workOrderId)
let workOrderData = record.load({
type: record.Type.WORK_ORDER,
id: workOrderId,
});
log.debug("workOrderData", workOrderData)
var salesOrderID = workOrderData.getValue({
fieldId: "createdfrom"
})
log.debug("salesOrderID", salesOrderID)
let salesOrderData = record.load({
type: record.Type.SALES_ORDER,
id: salesOrderID,
isDynamic: true
});
log.debug("salesOrderData", salesOrderData)
let lineCount = salesOrderData.getLineCount({
sublistId: 'item'
});
log.debug("lineCount", lineCount)
let soWorkOrderId, spoolNumber, spoolLength;
for (var i = 0; i < lineCount; i++) {
soWorkOrderId = salesOrderData.getSublistValue({
sublistId: 'item',
fieldId: 'woid',
line: i
});
log.debug("soWorkOrderId", soWorkOrderId)
// let test = soWorkOrderId.toString();
// log.debug("test",test)
let soItemDisplayName = salesOrderData.getSublistValue({
sublistId: 'item',
fieldId: 'item_display',
line: i
});
log.debug("soItemDisplayName", soItemDisplayName)
let soItemStart = (soItemDisplayName.length) - 2;
let soItemSubStr = soItemDisplayName.substring(soItemStart, soItemDisplayName.length);
log.debug("soItemSubStr", soItemSubStr)
if (soWorkOrderId.toString() === workOrderId.toString() && soItemSubStr.toUpperCase() === '-B') {
// if (soWorkOrderId.toString() === workOrderId.toString()){
log.debug("working***********")
spoolNumber = salesOrderData.getSublistValue({
sublistId: 'item',
fieldId: 'custcol10',
line: i
});
log.debug("spoolNumber", spoolNumber);
spoolLength = salesOrderData.getSublistValue({
sublistId: 'item',
fieldId: 'custcol11',
line: i
});
log.debug("spoolLength", spoolLength);
}
}
let workOrderLineCount = workOrderData.getLineCount({
sublistId: 'item'
});
log.debug("workOrderLineCount", workOrderLineCount)
for (var i = 0; i < workOrderLineCount; i++) {
let woItemDisplayName = workOrderData.getSublistValue({
sublistId: 'item',
fieldId: 'item_display',
line: i
});
log.debug("woItemDisplayName", woItemDisplayName)
var start = (woItemDisplayName.length) - 3;
let subStr = woItemDisplayName.substring(start, woItemDisplayName.length);
log.debug("subStr", subStr)
if (subStr.toUpperCase() === '-MR') {
// log.debug("correct", woItemDisplayName)
workOrderData.setSublistValue({
sublistId: 'item',
fieldId: 'custcol10',
line: i,
value: spoolNumber
});
workOrderData.setSublistValue({
sublistId: 'item',
fieldId: 'custcol11',
line: i,
value: spoolLength
});
}
}
workOrderData.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
}
} catch
(err) {
log.debug("Error@afterSubmit", err)
}
}
return {afterSubmit}
}
)
;