logo

NJP

Adding Variables to a Task via a script instead of using the slush bucket within a workflow for a Catalog Item

Import · Jan 20, 2020 · article

A question that I see come up from time to time would be, how do you add variables to be displayed on a task using a script instead of using the slush bucket on the activity task itself within the workflow editor? Why would you want or need to do this? There are a few instances that I've come across where knowing how to do this is useful.

One scenario is when you are actually creating the tasks themselves via a script, rather than as activities within the workflow. In this instance you don't have the option to use the slush bucket! One solution for this is to make whatever variables you want to display global, so that they appear on any tasks associated with the that catalog item, but this brings its own set of problems to the party.

Another instance I've encountered is when you want to display certain variables based on complex conditions. You could add every possible variable and then write UI policies or client-scripts to do the heavy lifting of hiding or displaying variables, but I personally find that cumbersome. Adding only the variables you want, based on the values when the task is created, makes more sense to me.

Last, but not least, you might find yourself in a situation where you don't know what variables you actually need on a task until after the task has already been created. Admittedly this one is rare, but on occasion I've found the need to add variables to tasks that are already in-flight, based on activity that is happening concurrently, which left me scratching my head at the time.

The basic process is simple to understand, once you've seen it. The core of this activity is creating a record within the sc_item_variables_task table, but to do so you'll need to know the sys_ids of your task, your RITM and each individual catalog item variable that you want to add to the task.

First you take your RITM (current) and query the item_option_new table for all records where the cat_item=current.cat_item and where name contains the list of variable names you want to add to the task.

Then you take the results of that query, plus the sys_id of the task, and create records in the sc_item_variables_task table and set task = task sys_id, and variable = the sys_id of each record you pulled from item_option_new. One thing to note, if you are doing this within the advanced script of a task, you will need to force the creation of your sys_id to make this work, which is actually easily done. Just call something like var taskID = task.setNewGuid(); and taskID will be the value you need.

Sounds a little confusing, but an example should make it somewhat clear. As I've used this multiple times, I personally use a script include to make the process repeatable and extensible. Below is my script include

var taskUtils = Class.create();
taskUtils.prototype = {
    initialize: function() {},

    addVariables: function(current, taskID, vars) {
        //current, pass the current record
        //taskID pass the ID of the task, obtained using task.NewGuid();
        //vars, comma seperated string with the names of the variables you want to add to a task

        //Query for the variables to add
        var getVar = new GlideRecord('item_option_new');
        getVar.addQuery('cat_item=' + current.cat_item + '^nameIN' + vars);
        getVar.query();

        //For each variable, create an associated record in sc_item_variables_task
        while (getVar.next()) {
            setVar = new GlideRecord('sc_item_variables_task');
            setVar.initialize();
            setVar.task = taskID;
            setVar.variable = getVar.getValue('sys_id');
            setVar.update();

        }

    },

    type: 'taskUtils'
};

Then, from within the advanced script section of a task, you can call something like this. In the example, I want to add the summary and description variables from the RITM to the task.

var taskID = task.setNewGuid();
var vars = 'summary,description';
var addObj = new taskUtils();
addObj.addVariables(current, taskID, vars);

And there you have it. Obviously this was a simple example, but using this as a template you would just write your script to check whatever conditions you are interested in and build a comma separated string that would contain the list of variable names that you want to add and then at the end call the code above.

If you found this article useful or helpful, please be kind a click appropriately! If you really found it useful, make bookmark it for later reference image

Edit:

Just to circle back as I've received this question a few times, it does not appear that you can add a multi-row variable set to a catalog task:

You cannot set Global as True for any variable that belongs to a multi-row variable set. So, a multi-row variable set is not available in catalog tasks.

Found here: https://docs.servicenow.com/bundle/orlando-it-service-management/page/product/service-catalog-manage...

I have not located a work-around for this limitation.

View original source

https://www.servicenow.com/community/developer-articles/adding-variables-to-a-task-via-a-script-instead-of-using-the/ta-p/2302808