When the inventory item is added to the’subitem of’ field, assign an item as a parent. then the parent item has a child’s custom checkbox ‘custitem_jj_haschildren checked as true. If the’subitem of’ field item is removed, we will check the parent item added inventory item search, and if the child items are greater than one, it will not be unchecked; otherwise, if the child item count is zero, we will uncheck the custom checkbox that has ‘custitem_jj_haschildren’.
/**
* @NApiVersion 2.0
* @NScriptType UserEventScript
* @NModuleScope SameAccount
* Author: Jobin & Jismi IT Services LLP
* Date: 17/10/2023
* Version: 2.0
*/
define(['N/record', 'N/log', 'N/search'], function (record, log, search) {
function afterSubmit(context) {
try {
if (context.type === context.UserEventType.EDIT) {
var oldRecord = context.oldRecord;
var newRecord = context.newRecord;
var childrenCount = []
var newHasChildren = newRecord.getValue({ fieldId: 'parent' });
var oldHasChildren = oldRecord.getValue({ fieldId: 'parent' });
if (newHasChildren !== oldHasChildren) {
if (newHasChildren != '' && newHasChildren != null) {
childrenCount = findChildrens(newHasChildren)
log.debug('before childrenCount', childrenCount)
if (childrenCount && childrenCount.length > 0) {
var fieldValues = { 'custitem_jj_haschildren': true }
record.submitFields({
type: record.Type.INVENTORY_ITEM,
id: newHasChildren,
values: fieldValues
});
} else {
var fieldValues = { 'custitem_jj_haschildren': false }
record.submitFields({
type: record.Type.INVENTORY_ITEM,
id: newHasChildren,
values: fieldValues
});
}
}
if (oldHasChildren != '' && oldHasChildren != null) {
childrenCount = findChildrens(oldHasChildren)
log.debug('after childrenCountold', childrenCount)
if (childrenCount && childrenCount.length > 0) {
var fieldValues = { 'custitem_jj_haschildren': true }
record.submitFields({
type: record.Type.INVENTORY_ITEM,
id: oldHasChildren,
values: fieldValues
});
} else {
var fieldValues = { 'custitem_jj_haschildren': false }
record.submitFields({
type: record.Type.INVENTORY_ITEM,
id: oldHasChildren,
values: fieldValues
});
}
}
}
}
} catch (error) {
log.debug('error', error)
}
}
/**
* @description The function is find the child items that parent of added subitem
* @param {object} id-data The id is the internalid of the parent item.
*/
function findChildrens(id) {
try {
var childObj = [];
var itemSearch = search.create({
type: search.Type.ITEM,
filters: [
["parent.internalid", "anyof", id]
],
columns: [
search.createColumn({
name: 'internalid'
}),
search.createColumn({
name: 'displayname'
}),
search.createColumn({
name: 'baseprice'
}),
search.createColumn({
name: 'itemid',
sort: search.Sort.ASC,
}) // Set the sorting order to ascending
]
});
itemSearch.run().each(function (result) {
childObj.push({
id: result.getValue({ name: 'internalid' }),
itemid: result.getValue({ name: 'itemid' }),
displayname: result.getValue({ name: 'displayname' }),
baseprice: result.getValue({ name: 'baseprice' })
});
return true;
});
return childObj;
} catch (error) {
log.debug('error', error)
}
// Now, you can work with the searchResultsArray.
}
return {
// beforeSubmit: beforeSubmit,
afterSubmit: afterSubmit
};
});