
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 02-11-2021 01:51 AM
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" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<style>
#content_row .reference-label {
padding-right: 15px;
}
#page_timing_div {
display: none;
}
</style>
<form class="form-horizontal">
<div class="form-group">
<label class="col-xs-4 control-label">
<span class="label-text" style="">${gs.getMessage('Cancel Reason')}</span>
</label>
<div class="col-xs-7 form-field input_controls">
<textarea required="true" class="form-control" value="cancelNotes" id="cancelNotes" type="text"></textarea>
</div>
</div>
<div class="form-group" style="padding-right:20px;margin-bottom:0;padding-left: 75%; line-height: 45px;">
<g:dialog_button id="cancel_button" type="button" style_class="$btn btn-default" onclick="actionCancel()" style="min-width: 5em;">${gs.getMessage('Cancel')}</g:dialog_button>
<g:dialog_button id="ok_button" type="button" onclick="actionOK()" style_class="btn btn-primary" style="min-width: 5em;">${gs.getMessage('OK')}</g:dialog_button>
<div class="clearfix"></div>
</div>
</form>
</j:jelly>
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,
- 3,841 Views