Need to filter Bank Accounts in custom record, based on the subsidiary selected in the same record.
Need this filtration in both create and Edit mode.
solution:
Add a virtual field in user event:
const beforeLoad = (scriptContext) => {
try {
//get the field Ids for the standard fields in the custom record
let recordBodyfields = getRecordBodyFieldsByType(scriptContext.newRecord.type);
if (scriptContext.type === scriptContext.UserEventType.CREATE && recordBodyfields.SUBSIDIARY) {
//Set the standard field to Normal, as the filed is created as inline in NetSuite
scriptContext.form.getField({ id: recordBodyfields.SUBSIDIARY }).updateDisplayType({
displayType: serverWidget.FieldDisplayType.NORMAL
});
}
if (scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.type === scriptContext.UserEventType.EDIT){
//Add the Virtual Field
let virtualAccountField = scriptContext.form.addField({
id: ‘custpage_bank_account’,
label: ‘BANK ACCOUNT’,
type: serverWidget.FieldType.SELECT,
});
//Set the standard field to hidden
scriptContext.form.getField({ id: recordBodyfields.BANK_ACCOUNT }).updateDisplayType({
displayType: serverWidget.FieldDisplayType.HIDDEN
});
if (scriptContext.type === scriptContext.UserEventType.EDIT) {
//Add filtration based on subsidiary in edit mode
let subsidiaryValue = scriptContext.newRecord.getValue({ fieldId: recordBodyfields.SUBSIDIARY });
let bankAccountvalue = scriptContext.newRecord.getValue({ fieldId: recordBodyfields.BANK_ACCOUNT });
//Set the actual bank account field value in virtual field
virtualAccountField.defaultValue = bankAccountvalue ? bankAccountvalue : ‘ ‘;
if (subsidiaryValue) {
//Get the active bank accounts using a search for the subsidiary
let activeBankAccountValues = getActiveBankCashAccounts(subsidiaryValue);
if (activeBankAccountValues && activeBankAccountValues.length > 0) {
activeBankAccountValues.forEach(account => {
virtualAccountField.addSelectOption({
value: account.internalid,
text: account.name,
});
});
}
}
}
}
} catch (error) {
log.error(‘Error @ beforeLoad’, error);
}
}
Add FieldChanged in Clientscript for dynamic filteration of Subsidiary changes in creation:
function fieldChanged(scriptContext) {
try {
let currentRecord = scriptContext.currentRecord;
let sublistId = scriptContext.sublistId;
let batchRecordFields = jjConstants.batchHeaderFields();
if (scriptContext.fieldId === batchRecordFields.SUBSIDIARY) {
let subsidiaryId = currentRecord.getValue({
fieldId: batchRecordFields.SUBSIDIARY
});
let selectElement = currentRecord.getField({ fieldId: ‘custpage_bank_account’ });
// Clear existing options when subsidiary is changed
selectElement.removeSelectOption({ value: null });
// Get active accounts for the subsidiary using search
let activeBankAccounts = getActiveBankCashAccounts(subsidiaryId);
selectElement.insertSelectOption({
value: ‘ ‘,
text: ‘ ‘
});
activeBankAccounts.forEach(function (option) {
selectElement.insertSelectOption({
value: option.internalid,
text: option.name
});
});
}
} catch (e) {
log.error(‘Error @ fieldChanged’, e.message);
}
}