SimonMorris
ServiceNow Employee
ServiceNow Employee

As all good administrators and developers know, Laziness is a virtue that should be practiced, cherished and encouraged!

Taking a good hint from "The Lazy Project Manager" I've been on the lookout for ways to be productively lazy.

On the ServiceNow forums a chap by the name of Reanimate asked:


I want to create an Incident UI Action (List choice) to create a Problem from checked Incidents.

I.e. check a number of incidents, select the UI Action "Create Problem" - ideally capture a description from the user then create a problem and relate all the checked incidents to the newly created problem.


OBVIOUSLY a man after my own heart. He's far too lazy to create that Problem record and go and manually relate all of the Incidents.

This is what he was after. As an Incident Manager you start to see multiple Incidents stacking up that have the same root cause, in this case a problem with SAP. How best to tie this information into a single Problem record.



YouTube link

So, to free up more of Reanimates day, so he has more time to do.. well, whatever he does.. here is how to do it.

You need 3 components to make this work.

  • A new view on the Problem form - to make the interface more concise and relevant for a quick entry of a Problem record
  • A Script Include to read and write the Incident records, and associate them to the Problem record
  • A UI Action to hold it all together


The new view



Lets cut down the default Problem form to only show the bare minimum needed to create a new Problem.

I've called the view Create and Relate - remember the name of the view you create so that you can reference it in the UI action later.

find_real_file.png


find_real_file.png

Click to enlarge the images.

The Script Include



This code lives on the ServiceNow server listening for calls to the AssociateIncidentsToProblems AJAX call.

When it gets a call it takes the sys_id of the Problem record that just been created, and a list of Incident sys_ids.

It then loops through the Incidents making the necessary association. You can download this Script Include at the bottom of this post.



var AssociateIncidentsToProblems = Class.create();

AssociateIncidentsToProblems.prototype = Object.extendsObject(AbstractAjaxProcessor, {

associate: function() {

var problem_sysid = this.getParameter("sysparm_problem_sysid");
var incident_list = this.getParameter("sysparm_incident_list");

var incident_array = incident_list.split(',');
for (var x = 0; x < incident_array.length; x++) {

var inc = new GlideRecord('incident');
inc.get(incident_array[x]);
inc.problem_id = problem_sysid;
inc.update();
}

var r = this.newItem();
r.setAttribute('result', 'success');
}
});


The UI Action



The UI action on the Incident table holds all of this together. I've set the type to be List Choice, and also told it to run on the Client side.

find_real_file.png

The code looks like this:



var CreateAndRelateCallback = function(response) {
var resp = response.responseXML.getElementsByTagName('item');

if (resp[0].getAttribute('result') != 'success'){
alert("Something went wrong with associating these Incidents to a Problem");
} else {
var win = getMainWindow();
reloadWindow(win);
}
}

var CreateAndRelate = function(action_verb, sys_id, updated_table, display_value) {

var ga = new GlideAjax('AssociateIncidentsToProblems');
ga.addParam('sysparm_name', 'associate');
ga.addParam('sysparm_problem_sysid', sys_id);
var list = GlideList2.get('incident');
ga.addParam('sysparm_incident_list', list.getChecked());
ga.getXML(CreateAndRelateCallback);
}

var CreateAndRelateForm = function() {

var dialog = new GlidePaneForm("Create a Problem", "problem", document.body, CreateAndRelate);
dialog.addParm('sysparm_view', "create_and_relate");
dialog.addParm('sysparm_form_only', 'true');
dialog.render();
}


Basically there is an initial function (CreateAndRelateForm) that is called when the UI Action is clicked.

Note that the new Problem form view is referenced here - and it uses the Name of the view and not the Title. I.e. "Create and Relate" == 'create_and_relate'.

Check in System UI > Views if you aren't sure of the name.

CreateAndRelateForm specifes CreateAndRelate as a callback to run when the Problem form is submitted.

CreateAndRelate creates the AJAX client to talk to the Script Include we saw above. When the XML comes back from the server it passes data into CreateAndRelateCallback to throw an error if anything went wrong, and finally to reload the form.

There you go! Not so lazy to write, but once you've set it up associating Incidents to Problems should be laaaazy!

Thanks to James Grinter for the help in debugging my namespace issues 🙂

11 Comments