We are trying to make 2 fields mandatory but we ar...
# suitescript
l
We are trying to make 2 fields mandatory but we are getting an “empty invocation target” error if the field is hidden from the form. Is it possible to avoid the error even if the field is hidden? Script in the reply section.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record', 'N/ui/serverWidget'],
function (record, serverWidget) {
function beforeLoad(context) {
try {
var form = context.form;
var sublistIds = ['item', 'expense', 'line'];
function setMandatoryFields(sublistId) {
var sublistObj = form.getSublist({ id: sublistId });
if (sublistObj) {
var financialLocationField = sublistObj.getField({ id: 'cseg1' });
var maxUnitField = sublistObj.getField({ id: 'cseg2' });
if (financialLocationField) {
financialLocationField.isMandatory = true;
}
if (maxUnitField) {
maxUnitField.isMandatory = true;
}
}
}
sublistIds.forEach(setMandatoryFields);
} catch (err) {
log.error({
title: 'ERROR: ' + err.name,
details: err.message
});
}
}
return {
beforeLoad: beforeLoad
};
});
n
Check if you have the field object before trying to do anything with it when you do .getField
👍 1
b
Sorry to reply to such an old thread, but can you elaborate on "check if you have the field object"? A non-null field is returned so the null check as shown in the example above doesn't prevent the error, and it seems like every property throws the reference error. How do I express "is this non-null object actually valid"?