Installation and Setup > Customizing ServiceMax > Custom JavaScript Snippets Support > Sample Code Snippets > Scenario 1A: Display error message in the respective fields on the child section
Scenario 1A: Display error message in the respective fields on the child section
The following JavaScript Code Snippet checks if the Start Date time is empty and displays a validation error on the Start Date time field of the Work Detail object. Configure the following Code snippet on the End Date Time field onChange Field Event.
function fromEntries(iterable) {
return [...iterable].reduce((obj, [key, val]) => {
obj[key] = val
return obj
}
, {
})
}

function keyBy(arr, key) {
return arr.reduce(function(map, obj) {
map[obj[key]] = obj;
return map;
}
, {
});
}


$event.get(function(rec) {
var request = {
};
debugger;
var parsedRec = JSON.parse(rec);

var startTimeSelected = parsedRec.currentRec['SVMXC__Start_Date_and_Time__c'];

$sfm_records.get(sfmData => {
if (sfmData) {
sfmData = JSON.parse(sfmData);

var header = sfmData.header;
var details = sfmData.details;
request.header = request.header || {
};


if (details) {


var mungedChildren = fromEntries(
// convert to array, map, and then fromEntries gives back the object
Object.entries(details).map(([key, value]) => [key, keyBy(value.lines, 'Id')])
);

var childValidations = fromEntries(
// convert to array, map, and then fromEntries gives back the object
Object.entries(mungedChildren).map(([k, v]) => [k, fromEntries(
Object.entries(v).map(([key, value]) => {

// Actual validation code
if (value.Id == parsedRec.currentRec['Id']) {
if (startTimeSelected && startTimeSelected.fieldvalue.value) {
// set end date if needed
} else {
// This field is in validation error
return [key, ['SVMXC__Start_Date_and_Time__c']]
}
}
return null;

}
).filter((each) => each !== null)
)]).filter((each) => Object.keys(each[1]).length > 0)
);

$response({
'status': 'fail',
'error_message': 'Start Date Time was Empty',
'validationType': 'ERROR',
'relatedChildFields': childValidations,
}
);


}
}
else {
$response({
'status': 'fail',
'error_message': 'No Sfm Data'
}
);
}
}
);
}
);
Was this helpful?