Michael Jones -
Giga Sage

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 🙂

 

 

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. 

Comments
Shane J
Tera Guru

Michael - you've got a typo in here that will cause issues:

addVariables: function(current, taskid, vars) {

should read

addVariables: function(current, taskID, vars) {

 

To help anyone with how to use this in a script that creates Cat Tasks, here's an example from mine:

...

grSCTask.insert();

//Show the variables on child Catalog Tasks
var taskID = grSCTask.sys_id;
var vars = 'business_reason';
var addObj = new taskUtils();
addObj.addVariables(current, taskID, vars);

...

 

EDIT:  Fixed so you don't cause duplicate Tasks if you use my example...

Michael Jones -
Giga Sage

Thanks! Corrected. 

ohhgr
Kilo Sage
Kilo Sage

Great article @Michael Jones - CloudPires ....

D N
Tera Contributor

@Michael Jones - CloudPires How would you display a multi-row variable set on the Catalog task that is shown on the on the Request Item record using scripting? 

Michael Jones -
Giga Sage

Sorry for the late response; I've looked into this and I haven't...quite...figured out how to make it work with a multi-row variable set. They follow a similar pattern, but are just different enough that the process doesn't seem to work. If I find a solution I'll post it back here! 

Bartosz Nowak2
Kilo Guru

Hi @Michael Jones - CloudPires ,

have you figured out the MRVS on SC_TASK for this ?

Thanks,

Bartosz

Michael Jones -
Giga Sage

Just to circle back, 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. 

Michael Jones -
Giga Sage

Just to circle back, 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. 

Shane J1
Tera Contributor

Michael - I just realized this doesn't work for any Variables that are part of a Variable Set.

I don't suppose you've already overcome this and could post an updated script?

Shane J1
Tera Contributor

After some major borrowing from jnovack here:

https://community.servicenow.com/community?id=community_question&sys_id=0280cfe1db98dbc01dcaf3231f961956

 

This Update version of the Script include appears to grab both Variables AND Variable Set variables.

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.insert();

            //start - insert all catalog item variable-set variables on task form 
            var variableSets = new GlideRecord('io_set_item');
            variableSets.addQuery('sc_cat_item', current.cat_item);
            variableSets.query(); // looking for io_set_item.variable_set == item_option_new_set.sys_id
            while (variableSets.next()) { // iterate through variable sets on catalog item
                var setVariables = new GlideRecord('item_option_new_set');
                setVariables.addQuery('sys_id', variableSets.variable_set);
                setVariables.query();
                while (setVariables.next()) { // iterate through each variable set
                    var catsetVariables = new GlideRecord('item_option_new');
                    catsetVariables.addQuery('variable_set', setVariables.sys_id);
                    catsetVariables.addQuery('type', 'NOT IN', '280d44163720300054b6a3549dbe5d3c,ec0d44163720300054b6a3549dbe5d3c,311dc651c3121100c8b837659bba8fc4,ac0d44163720300054b6a3549dbe5d3c');
                    // Type is NOT (Break, Container End, Container Split, Container, Start)
                    catsetVariables.addQuery('name', 'IN', vars);
                    catsetVariables.query();
                    while (catsetVariables.next()) { // iterate through variables in variable set
                        var mysetVar = new GlideRecord('sc_item_variables_task');
                        mysetVar.initialize();
                        mysetVar.task = taskID;
                        mysetVar.variable = catsetVariables.sys_id;
                        mysetVar.insert();
                    }
                }
            }
            //end - insert all catalog item variable-set variables on task form
        }
    },
    type: 'taskUtils'
};

 

Joe_M
Tera Contributor

Hi All, 

I recently had a need to add variables to a catalog task via workflow run script and wanted to share how I accessed the item_option_new table to insert records into sc_item_variables_task.

Same Process for the run script within workflow, but for the script include you can use sc_item_option_mtom for all the details you need for item_option_new. By using sc_item_option_mtom you will be able to use variables that are stand-alone as well as variables that are part of a variable set in a single query.

 

var taskUtils = Class.create();
taskUtils.prototype = {
    initialize: function() {},
    addVariables: function(current, taskID, vars) {
        /* workflow will need to provide the following details to utilize addVariables function
        var taskID = task.insert(); //used to grab the sysId of the target task during insert
        var vars = 'x_wh_delivery,x_user_pickup'; //comma seperated list of variables to add
        var addObj = new taskUtils(); //calls the taskUtils Script Include
        addObj.addVariables(current, taskID, vars); //input parameters */
        var sc = new GlideRecord('sc_item_option_mtom');
        sc.addQuery('request_item', current.sys_id);
        sc.addQuery('sc_item_option.item_option_new.nameIN' + vars);
        sc.query();
        while (sc.next()) {
            setVar = new GlideRecord('sc_item_variables_task');
            setVar.initialize();
            setVar.task = taskID;
            setVar.variable = sc.sc_item_option.item_option_new.getValue('sys_id');
            setVar.insert();
        }
    },
    type: 'taskUtils'
};
Scott Jordheim
Giga Guru

Ah, I do something similar, but just query sc_item_option in between the mtom table and sc_item_variables_task glide record calls to get all variables associated to the RITM, rather than picking and choosing which ones to pass. Seems to work good for me in adding variables and MRVS.

As it seems fairly easy to do this via scripting, not sure why SNow hasn't figured out how to do it via UI yet for MRVS.

RBlor
Mega Guru

Hello! trying to do something similar but copy variables from a record producer to a 2nd incident in workflow via create task activity.  would the script be similar? and is the first script placed on a runscript activity after the create task activity?

Paul Dowsett
Tera Expert

Hi Scott,

Would you be so kind as to provide a code example of what you did to include MRVS?  I have managed to show regular variable sets using Joe's example (thanks Joe!), just not the MRVS.

Many thanks

Paul

Joe_M
Tera Contributor

Hi Paul,


I felt adventurous today, and was curious how to get this MRVS into the sc_item_variables_task table.

Turns out there is something built in to the interface that checks the variable(s) you are adding to the catalog task to see if they are part of the MRVS. 

If the variable is, it will display the entire MRVS even if only one variable from that set is present. 

You can use the above script to include a variable in that MRVS to the record in your vars statement to get this to display.

You can quickly no code test it out by finding a catalog task in sub prod where there is a MRVS associated with the catalog item. Filter your sc_item_variables_task for that task and record the values for one of the records whos variable is part of the MRVS. Delete all the records, refresh your task and validate all the variables are not showing for the task.

Create a new record for the task in the sc_item_variables_task table and fill in the values you recorded.

In my testing on Quebec it displayed the entire MRVS on the sc_task, even though only one variable from the set was in the sc_item_variables_task table for that task.

Enjoy!

Joe

Paul Dowsett
Tera Expert

Hi Joe,

Funny I discovered this shortly after posting.  I compared tasks created with the OOTB Catalog Task activity vs Run Script and noticed only the child variables from the MRVS were registered in sc_item_variables_task even though the MRVS internal name was specified in the variable slushbucket of the Catalog Task activity.

Although this does work (for now) specifying just the one field seems a bit dubious if you ask me, so I plan to specify all the child variables to be safe.

Thanks for checking!

Paul

Aman Sahu
Tera Contributor

Hi,

This is my Run Script in the workflow for creating n number of tasks and adding some variables.

I don't able to understand why it's not working.

 

I have used the same script include mentioned above.

Here is my code for run script:

var task = new GlideRecord('sc_task');


for (var i = 0; i < current.variables.count_of_laptops; i++) {


    task.initialize();


    task.short_description = "Installation task for Laptops" + " - " + current.u_recipient_name.name + " - " + current.u_recipient_name.u_country.getDisplayValue();
    task.u_task_business_days = '10';
    task.assignment_group = current.variables.select_assignment_group_laptops;


    task.request_item = current.sys_id;
	
    task.insert();

    //adding variables
    var taskID = task.setNewGuid();
    var vars = 'description,available_laptop,subcategory';
    var addObj = new taskUtils();
    addObj.addVariables(current, taskID, vars);
	

}
Bryan Chang
Tera Contributor

Thanks for sharing this solution! It helps me to resolve my problem greatly.

JayGervais
Kilo Sage

Try changing 

var taskID = task.setNewGuid();

to

var taskID = task.insert();
Version history
Last update:
‎01-20-2020 07:05 AM
Updated by: