Cancel Incident(s) from List View with Mandatory Cancel Reason
Hi All,
Recently, I have received a requirement from one of the customers that they want to cancel the Incident or multiple incident from the list view with mandatory cancel reason to pop up during cancelation.
I thought of sharing this as this might be useful to many. Please follow below steps to accomplish this task.
We need 5 activities to be performed.
Activities:-
1. Create 'Cancel Reason' field.
2. Create the Script Include.
3. Create a UI Page.
4. Create the UI Action.
5. Create the Client script.
Let's have a look at each activity.
1. Create 'Cancel Reason' field for storing the cancel reason for Incident record.
2. Create a client callable Script Include 'IncidentCancel' as below.
I am pasting the above code below
var IncidentCancel = Class.create();IncidentCancel.prototype = Object.extendsObject(AbstractAjaxProcessor, { cancelIncidents: function() { var selectedIncidents = this.getParameter("sysparm_incidents"); var notes = this.getParameter("sysparm_cancelNote"); var incidentGr = new GlideRecord('incident'); incidentGr.addQuery('sys_id', 'IN', selectedIncidents); incidentGr.addEncodedQuery('stateNOT IN7,8'); incidentGr.query(); while (incidentGr.next()) { incidentGr.setValue('state', 8); incidentGr.setValue('u_cancel_reason', notes); incidentGr.update(); } return true; }, type: 'IncidentCancel'
});
3. Create a UI Page 'mandatory_fields_to_cancel_incident_ui' for showing prompt box.
Please use below HTML and Client Script Code.
HTML
<?xml version="1.0" encoding="utf-8" ?> #content_row .reference-label { padding-right: 15px;</p> <p>}</p> <h1>page_timing_div { display: none; }
${gs.getMessage('Cancel')}/g:dialog_button
${gs.getMessage('OK')}/g:dialog_button
Client Script
function actionOK() { var modal = GlideModal.prototype.get("mandatory_fields_to_cancel_incident_ui"); var incidents = modal.getPreference("selected_incidents"); if (incidents) { //close the incidents var glideAjax = new GlideAjax("IncidentCancel"); glideAjax.addParam("sysparm_name", "cancelIncidents"); glideAjax.addParam("sysparm_incidents", incidents); glideAjax.addParam("sysparm_cancelNote", $("cancelNotes").value); glideAjax.getXMLAnswer(function(answer) { GlideModal.prototype.get("mandatory_fields_to_cancel_incident_ui").destroy(); GlideList2.get('incident').refresh(); }); }
}
function actionCancel() { GlideModal.prototype.get("mandatory_fields_to_cancel_incident_ui").destroy();
}
(function() { var okButton = gel('ok_button'); var cancelNotesEl = gel('cancelNotes'); okButton.disabled = true; cancelNotesEl.value = ""; cancelNotesEl.on('input', function() { if (cancelNotesEl.value !=="") okButton.disabled = false; else okButton.disabled = true; });
})();
4. Create the UI Action 'Cancel incidents' to cancel Incidents which are neither Closed or Cancelled, otherwise we might end up cancelling already closed or cancelled incidents.
- Name: Cancel incidents
- Table: Incident [incident]
- Show update: Select the check box
- List choice: Select the check box
- List v2 Compatible: Select the check box
- Client: Select the check box
- Onclick: cancelIncidents()
- Condition: current.getValue('state') !'7' && current.getValue('state') !'8'
Paste the below script
function cancelIncidents() { var list = GlideList2.get('incident'); var title = list.getTitle(); var incidents = list.getChecked(); if (incidents) { var o = new GlideModal('mandatory_fields_to_cancel_incident_ui'); getMessage("Cancel Incidents", function(msg) { o.setTitle(msg); o.setPreference('selected_incidents', incidents); o.render(); }); }
}
Output:
Before we complete the final activity, lets check the output for cancelling Incidents from Actions list.
Now let's cancel the individual incident by using onCellEdit client script.
5. Create 'Cancel incident state from List' client script of type onCellEdit.
Paste below script.
function onCellEdit(sysIDs, table, oldValues, newValue, callback) { var saveAndClose = true;
//Type appropriate comment here, and begin script below
if (newValue == "8") { var incidents = sysIDs; if (incidents) { var o = new GlideModal('mandatory_fields_to_cancel_incident_ui'); getMessage("Cancel Incidents", function(msg) { o.setTitle(msg); o.setPreference('selected_incidents', incidents); o.render(); }); } }
}
Result:
With this we have covered all the activities.
Thanks,
Labels:
https://www.servicenow.com/community/itsm-articles/cancel-incident-s-from-list-view-with-mandatory-cancel-reason/ta-p/2307396
