define(['N/record', 'N/search'],
/**
* @param{record} record
*/
function (record, search) {
function getItemList(subsidiary) {
try {
let itemArray = []
var itemSearchObj = search.create({
type: "item",
filters:
[
["type", "anyof", "InvtPart", "NonInvtPart", "OthCharge", "Service"],
"AND",
["subsidiary", "is", subsidiary],
"AND",
["isinactive", "is", "F"],
"AND",
["subtype", "anyof", "Resale", "Sale", "@NONE@"]
],
columns:
[
search.createColumn({ name: "itemid", label: "Name" }),
search.createColumn({ name: "internalid", label: "Internal ID" }),
search.createColumn({ name: "displayname", label: "Display Name" }),
search.createColumn({ name: "salesdescription", label: "Description" }),
search.createColumn({ name: "type", label: "Type" }),
search.createColumn({ name: "baseprice", label: "Base Price" }),
]
});
var searchResultCount = itemSearchObj.runPaged().count;
console.log("itemSearchObj result count", searchResultCount);
itemSearchObj.run().each(function (result) {
itemArray.push({
itemId: result.getValue({ name: "internalid", label: "Internal ID" }),
itemName: result.getValue({ name: "itemid", label: "Name" })
})
// .run().each has a limit of 4,000 results
return true;
});
return itemArray
}
catch (e) {
log.error('error @GetItemList', e)
}
}
/**
* Function to be executed after page is initialized.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
*
* @since 2015.2
*/
// function pageInit(scriptContext) {
// }
/**
* Function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @since 2015.2
*/
function fieldChanged(scriptContext) {
try {
let recordObj = scriptContext.currentRecord;
let fieldChanged = scriptContext.fieldId;
// Check if the changed field is `subsidiary`
if (fieldChanged === 'subsidiary') {
let subsidiaryValue = recordObj.getValue({ fieldId: 'subsidiary' });
if (subsidiaryValue) {
console.log('Selected Subsidiary:', subsidiaryValue);
// Fetch the custom sublist field
let customItemField = recordObj.getSublistField({
sublistId: 'recmachcustrecord_jj_pricelevel_customer',
fieldId: 'custpage_custom_items',
line: 0 // Specify a valid line number if applicable
});
if (customItemField) {
// Clear existing options
let optionCount = customItemField.getSelectOptions().length;
for (let i = optionCount - 1; i >= 0; i--) {
customItemField.removeSelectOption({
value: customItemField.getSelectOptions()[i].value
});
}
// Add default option
customItemField.insertSelectOption({ value: '', text: 'Select Item' });
// Fetch item details for the subsidiary
let itemDetailList = getItemList(subsidiaryValue);
console.log('Item Details:', itemDetailList);
// Populate the custom field with item options
itemDetailList.forEach((item) => {
customItemField.insertSelectOption({
value: item.itemId,
text: item.itemName
});
});
}
}
}
} catch (e) {
console.error('Error @fieldChanged:', e);
}
}
this script sample sets the value of the custom sublist field item based on the saved search value.