How to add variable on catalog task using run script workflow

SHALIKAS
Tera Guru

I am using the below script

var scGr = new GlideRecord('sc_task');
            scGr.initialize();
            scGr.request_item = current.sys_id;
            
             
                    scGr.short_description = 'Task for Email All data collection.';
                    scGr.description = 'Task for Email All data collection. For the following Custodian: ' 
                scGr.insert();
var catVariables = new GlideRecord('item_option_new');
            catVariables.addQuery('cat_item', current.cat_item);
           
            catVariables.query();

            while (catVariables.next()) {
                var myVar = new GlideRecord('sc_item_variables_task');
                myVar.initialize();
                myVar.task = task.sys_id;
                myVar.variable = catVariables.sys_id;
                myVar.insert();
            }
But it is not working

       
           
       
1 ACCEPTED SOLUTION

iekosmadakis
Mega Sage

Hello @SHALIKAS !

Your current script fails in this line: myVar.task = task.sys_id;
task is undefined in this context. You just inserted the new Catalog Task record into scGr, so you must reference scGr.sys_id (or store that value in a variable) before you loop.

Please try this code. I adjusted your code a bit and fixed some common issues without changing its scope.

// 1. Create the Catalog Task
var taskGR = new GlideRecord('sc_task');
taskGR.initialize();
taskGR.request_item = current.sys_id; // Link to RITM
taskGR.short_description = 'Task for Email All data collection';
taskGR.description = 'Task for Email All data collection. For the following Custodian: ';
var taskSysId = taskGR.insert(); // Commit & keep the sys_id

// 2. Pick the catalog variables you want to expose on the task
var varGR = new GlideRecord('item_option_new');
varGR.addQuery('cat_item', current.cat_item);
varGR.query();

// 3. Link each variable to the task
while (varGR.next()) {
    var linkGR = new GlideRecord('sc_item_variables_task');
    linkGR.initialize();
    linkGR.task = taskSysId; // <-- FIXED
    linkGR.variable = varGR.getUniqueValue(); // sys_id of item_option_new
    linkGR.insert();
}


Please consider marking my answer as helpful and accepting it as the solution if it assisted you in any way.

View solution in original post

3 REPLIES 3

iekosmadakis
Mega Sage

Hello @SHALIKAS !

Your current script fails in this line: myVar.task = task.sys_id;
task is undefined in this context. You just inserted the new Catalog Task record into scGr, so you must reference scGr.sys_id (or store that value in a variable) before you loop.

Please try this code. I adjusted your code a bit and fixed some common issues without changing its scope.

// 1. Create the Catalog Task
var taskGR = new GlideRecord('sc_task');
taskGR.initialize();
taskGR.request_item = current.sys_id; // Link to RITM
taskGR.short_description = 'Task for Email All data collection';
taskGR.description = 'Task for Email All data collection. For the following Custodian: ';
var taskSysId = taskGR.insert(); // Commit & keep the sys_id

// 2. Pick the catalog variables you want to expose on the task
var varGR = new GlideRecord('item_option_new');
varGR.addQuery('cat_item', current.cat_item);
varGR.query();

// 3. Link each variable to the task
while (varGR.next()) {
    var linkGR = new GlideRecord('sc_item_variables_task');
    linkGR.initialize();
    linkGR.task = taskSysId; // <-- FIXED
    linkGR.variable = varGR.getUniqueValue(); // sys_id of item_option_new
    linkGR.insert();
}


Please consider marking my answer as helpful and accepting it as the solution if it assisted you in any way.

PrashanthR95946
ServiceNow Employee
ServiceNow Employee

Hi @SHALIKAS First add variable editor on sc task form view.

 

later go to the flow or workflow which ever you are using.

if flow then open the catalog task action and scroll down there you see the variables in left slush bucket add them to right side then it will show on task form view.

 

if workflow then open the catalog task activity and scroll down there you see the variables in left slush bucket add them to right side then it will show on task form view.

you need to perform this for each task.

 

Thanks

Prashanth

 

Please mark the answer correct & Helpful, if i could help you.

Chaitanya ILCR
Kilo Patron

Hi @SHALIKAS 

there are few way you can achieve this

1. use normal catalog task activity and add all the variables to the list collector

ChaitanyaILCR_0-1750182046051.png

 

2. check the global checkbox in the variables

 

ChaitanyaILCR_1-1750182188854.png

 

these will display the variables on the sc_task form

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya