RE: How to add options in sublist select type custom field?
I have created custom select type department field on a journal entry’s sublist through user event before load script. I am getting context.mode value as blank. Now I am getting departments using field lookup from account record’s department field which is multiselect type. I am trying to insert those departments into journal entries custom department field using client script validate field event. But its adding same options on all lines of custom department field. Also When I try to add account on new line,then I am getting an error as “”type”:”error.SuiteScriptError”,”name”:”SSS_INVALID_SUBLIST_OPERATION”,”message”:”CurrentRecord.getSublistField”,”id”:null,”stack”:[“SSS_INVALID_SUBLIST_OPERATION: CurrentRecord.getSublistField”,” at Object.createError [as nlapiCreateError]
Please help!!
The error message “SSS_INVALID_SUBLIST_OPERATION: CurrentRecord.getSublistField” indicates that you are trying to use the CurrentRecord.getSublistField()
method on a sublist that does not exist. This can happen if you are trying to access a sublist on a record that is not a sublist record, or if the sublist name is incorrect.
In your case, you are trying to use the CurrentRecord.getSublistField()
method to access the custpage_department
field on the line
sublist. However, the custpage_department
field is not a sublist field. It is a custom field that you have added to the journal entry record.
To fix this error, you need to remove the CurrentRecord.getSublistField()
method from your script. You can then access the custpage_department
field directly using the CurrentRecord.getField()
method.
Here is an updated version of your script that fixes the error:
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
*/
define(['N/record', 'N/search', 'N/ui/dialog'],
function (record, search, dialog) {
function validateField(context) {
try {
var currRec = context.currentRecord;
var sublistName = context.sublistId;
var sublistFieldName = context.fieldId;
if (sublistName === 'line' && sublistFieldName === 'account') {
// Get account value.
var accountid = currRec.getCurrentSublistValue({
sublistId: 'line',
fieldId: 'account'
});
// Get the account's department restrictions.
var fieldLookUp = search.lookupFields({
type: search.Type.ACCOUNT,
id: accountid,
columns: ['custrecord_cp_account_restrict_dept', 'custrecord_cp_dept_selection']
});
var restrict = fieldLookUp.custrecord_cp_account_restrict_dept;
var restrictDept = fieldLookUp.custrecord_cp_dept_selection;
// If the account's department restrictions are not enabled, then populate the
// custom department field with the restricted departments.
if (!restrict) {
// Get the custom department field.
var customDepartmentField = currRec.getField({
fieldId: 'custpage_department'
});
// Remove all existing options from the custom department field.
customDepartmentField.removeSelectOption({ value: null });
// Add the restricted departments to the custom department field.
for (var j = 0; j < restrictDept.length; j++) {
customDepartmentField.insertSelectOption({
value: restrictDept[j].value,
text: restrictDept[j].text
});
}
}
} else if (sublistName === 'line' && sublistFieldName === 'custpage_department') {
// Get the custom department field value.
var customDepartment = currRec.getFieldValue({
fieldId: 'custpage_department'
});
// Set the department field value to the custom department field value.
currRec.setFieldValue({
fieldId: 'department',
value: customDepartment
});
}
} catch (e) {
log.error('Error occurred: ', e);
}
return true;
}
return {
validateField: validateField
};
});
This updated script will now populate the custom department field with the restricted departments when the user enters an account on a new line. It will also set the department field value to the custom department field value when the user validates the custom department field.