function onChange(control, oldValue, newValue, isLoading, isTemplate) {
// NOTE: the Close Incomplete UI action on HR Case depends on
// this client script's behavior
// If state propagation is stopped, don't proceed, just return
if (g_scratchpad.stopIncompleteStatePropagation) {
g_scratchpad.stopIncompleteStatePropagation = false;
return;
}
//Live Update check not available in Printer friendly version
var isLiveUpdating = typeof g_form.isLiveUpdating === 'function' && g_form.isLiveUpdating();
if (isLoading || newValue === '' || isLiveUpdating) {
g_scratchpad.previous_state = newValue;
return;
}
// If calling from workspace
if (typeof(g_modal) !== 'undefined') {
// form field set to close incomplete? can be set from form edit or UI action
if (newValue == 4) {
// Go get the Suspend reasons from backend
var ga = new GlideAjax('sn_hr_core.irm_hr_CaseAjax');
ga.addParam('sysparm_name', 'getCloseIncompleteReasons');
ga.addParam('sysparm_table_name', g_form.getTableName());
ga.addParam('case_id', g_form.getSysId());
ga.getXML(wsParseCloseIncompletedReasons);
}
// Got the reasons we care about, now plug those values into choice field, and pop the dialog box
} else {
// If the state is changed to Close Incomplete (4)
if (newValue == 4) {
// Revert back to the previous state and show the close incomplete dialog
setIncompleteState(g_scratchpad.previous_state);
var sysId = typeof rowSysId == 'undefined' || rowSysId == null ?
g_form.getUniqueValue() : rowSysId;
var dialogClass = GlideModal ? GlideModal : GlideDialogWindow;
var dialog = new dialogClass('sn_hr_core_irm_closeIncompleteDialog');
dialog.setTitle(getMessage('Close Incomplete'));
dialog.setPreference('sysparm_sys_id', sysId);
dialog.setPreference('sysparm_ok_button_name', getMessage('Close Incomplete'));
dialog.setPreference('focusTrap', true);
dialog.setPreference('sysparm_table_name', g_form.getTableName());
dialog.on('closeIncompleteSuccess', function() {
dialog.destroy();
gsftSubmit(null, g_form.getFormElement(), 'sysverb_update_and_stay');
});
dialog.render();
return;
}
}
function wsParseCloseIncompletedReasons(response) {
var messages = ['reason_code', 'Close Incomplete'];
getMessages(messages, function() {
var answer = response.responseXML.documentElement.getAttribute("answer");
var reasons = JSON.parse(answer);
if (!reasons)
reasons = [];
var fields = [{
type: 'choice',
name: 'reason',
label: getMessage('reason_code'),
value: (reasons && reasons.length > 0) ? reasons[0].value : '',
choices: reasons,
mandatory: true
}
];
var sysID = g_form.getUniqueValue();
var tableName = g_form.getTableName();
g_modal.showFields({
title: getMessage('Close Incomplete'),
fields: fields,
size: 'md',
confirmType: 'confirm'
}).then(function(fieldValues) {
//get the work note entered
var newReason = fieldValues.updatedFields[0].value;
g_form.setValue("u_closure_reason", newReason);
g_form.addInfoMessage('Closure reason entered: ' + newReason);
setTimeout(function () { g_form.save(); }, 1000);
}, function() {
// Reset state dropdown if the modal is canceled
setIncompleteState(g_scratchpad.previous_state);
});
});
}
function setIncompleteState(value) {
// Stop the state propagation so that we don't go into an infinite loop
// within this client script
g_scratchpad.stopIncompleteStatePropagation = true;
g_form.setValue('state', value);
}
g_scratchpad.previous_state = oldValue;
}