Requirement
Based on the custom form selected from a select box, there are some fields whose display type can be false or true. On the save of that record, find all the custom fields whose display type is false and set those custom fields to empty.
Solution
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord', 'N/record', 'N/search'],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param{search} search
*/
function(currentRecord, record, search) {
/**
* 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) {
try {
var record = nlapiCreateRecord('customrecord_jj_store_visit');
var fields = record.getAllFields(); //Get all the fields
of a custom record
var cust_array = []; //Array to store all the custom fields
for (var i = 0; i < fields.length; i++) {
if ((fields[i].startsWith("cust"))){
cust_array.push(fields[i])
}
}
var objRecord = currentRecord.get(); //Get currentrecord
//Loop through custom fields array to check the display type and if the display type is false , set its value to ''
cust_array.forEach(function (element) {
if (!objRecord.getField({fieldId: element}).isDisplay) { //check DisplayType of field is false
if(objRecord.getField({fieldId: element}).type!=='checkbox'){ //check type not checkbox and set all those fieldvalue to null
objRecord.setValue({
fieldId: element,
value: ''
});
}
// else{ //To set the fields with type checkbox to false
// objRecord.setValue({
// fieldId: element,
// value: false
// });
// }
}
});
return true;
}
catch (e) {
console.log('Error in saverecord',e)
}
}
return {
saveRecord: saveRecord
};
});