Workflow Advanced Script

Maki
Tera Expert

Hello all,

last but not least i need help with a catalog task. I want to use the same worfklow (sc_request_item) with more items.

In this workflow is a catalog task, assigned to our ServiceDesk. 

My problem is to show the current variables in the task. 

For Example the workflow is in "item1" / "item2" / "item3". 
Item1 variables: test1 / test2
Item2 variables: snow1 / snow2
item 3 variables: sun1 / sun2

So all variables got different names. Is there a way via script, to check the current variables of the requested item?

This is what i got now:

task.short_description = current.cat_item.name; // working
task.variables = current.variables; //Does not work! nothing happens here!

 

12 REPLIES 12

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you will have to determine for which catalog item this workflow is running since you have same workflow

running for 3 different catalog items

sample script for reference Using if else statement.

Note: Ensure you use valid catalog item sys_ids for all 3 catalog items in the if condition during comparison

Also use proper variable names from each catalog items

The below should work

if(current.cat_item == 'item1SysId'){

task.description = current.variables.test1 + ' ' + current.variables.test2;

}

else if(current.cat_item == 'item2SysId'){

task.description = current.variables.snow1 + ' ' + current.variables.snow2;

}

else (current.cat_item == 'item3SysId'){

task.description = current.variables.sun1 + ' ' + current.variables.sun2;

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

ok thanks i will test this, but then i have to write a script with to many if conditions. Then i can also create a workflow for each item and use the variable editor 🙂

i thought i can do this automatically via script. In your example, i have to update the script, if there is a new item with the default workflow, right? 

You do not need to write so many if conditions.

Workflow context is for each RITM workflow.

You can create 1 script include and pass current catalog item sys_id to this script include.

Script include function will return variable values based on catalog item.

In future, if you add more catalog items with same workflow, then you just have to update script include and NOT your workflow.

 

Regards,

Sachin

ok not update the workflow himself, just the script inside. i hoped there is a easier way to do this. Some script that check the current variables and print them in the descripition, without manual update some scripts (human error -> when new item and someone forget to update the script)

DirkRedeker
Mega Sage

Hi

If you have different Items having different variables, you can try this:

var variables = current.variables.getElements(); 
    for (var i=0;i<variables.length;i++) { 
        var question = variables[i].getQuestion(); 
        gs.addInfoMessage(question.getLabel() + ":" + question.getValue()); 
    } 

This will "pipe out" the variables.

Current will be a GlideRecord on the Requested Item.

Try it out and let me know, if that answers your question and mark my answer as correct and helpful.

Enjoy & BR

Dirk