Server Side Scripts will be blocked when the field is made from the Form/Field setup mandatory. So we will make the field mandatory through the script only for those required times.
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord'],
function (currentRecord) {
var CR_AUTOSHIP_STATUS_FLD = 'custrecord_vv_autoship_status';
var CR_AUTOSHIP_ADDON_FLD = 'custrecord_vv_autoship_addon';
var CR_AUTOSHIP_SHIPDATE_FLD = 'custrecord_vv_autoship_date';
var CR_AUTOSHIP_FREQUENCY_FLD = 'custrecord_vv_autoship_freq';
var CR_AUTOSHIP_INTERVAL_FLD = 'custrecord_vv_autoship_interval';
var mandatoryFields = [
CR_AUTOSHIP_SHIPDATE_FLD,
CR_AUTOSHIP_FREQUENCY_FLD,
CR_AUTOSHIP_INTERVAL_FLD,
];
/**
* Validation function to be executed when record is saved.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @returns {boolean} Return true if record is valid
*
* @since 2015.2
*/
function saveRecord(scriptContext) {
var emptyFields = checkEmptyMandatoryFields(currentRecord, mandatoryFields);
if (emptyFields.length > 0) {
alert('Please enter value(s) for: ' + emptyFields.join(', '));
return false;
}
return true;
}
/**
* 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) {
revealMandatoryFields(scriptContext, currentRecord, mandatoryFields);
}
/**
* 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) {
if (scriptContext.fieldId === CR_AUTOSHIP_STATUS_FLD || scriptContext.fieldId === CR_AUTOSHIP_ADDON_FLD) {
revealMandatoryFields(scriptContext, currentRecord, mandatoryFields);
}
}
function revealMandatoryFields(scriptContext, curRec, fields) {
var lineRec = scriptContext.currentRecord;
// validate mandatory fields are filled
// when status is Active
if (lineRec.getText(CR_AUTOSHIP_STATUS_FLD) === 'Active') {
// Date is the only mandatory field for Add-On
if (lineRec.getValue(CR_AUTOSHIP_ADDON_FLD)) {
toggleMandatoryFields(curRec, [CR_AUTOSHIP_SHIPDATE_FLD], true);
toggleMandatoryFields(curRec, [CR_AUTOSHIP_FREQUENCY_FLD, CR_AUTOSHIP_INTERVAL_FLD], false);
} else {
toggleMandatoryFields(curRec, fields, true);
}
} else {
toggleMandatoryFields(curRec, fields, false);
}
}
function checkEmptyMandatoryFields(record, fields) {
var emptyFields = []
fields.forEach(function (fieldId) {
var field = record.get().getField(fieldId);
if (field.isMandatory && record.get().getValue(fieldId) === '') {
emptyFields.push(field.label)
}
});
return emptyFields;
}
function toggleMandatoryFields(record, fields, isMandatory) {
fields.forEach(function(fieldId) {
var field = record.get().getField(fieldId);
field.isMandatory = isMandatory;
return true;
});
}
return {
saveRecord: saveRecord,
pageInit: pageInit,
fieldChanged: fieldChanged,
};
});