Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Creating separate tasks for each value in List Collector

Dawid2
Giga Guru

Hello,

Would it be possible to create a Workflow that would create X number of tasks, where X is the number of items selected in List Collector variable? For example, if user selected 2 items in List Collector, it should create 2 tasks, if 3 - then 3 tasks should be created. I found the script provided by @Mark Stanger that reads:

 

// Create 'sc_task' records for each item in the list collector var
var options = current.variables.list_collector;

// Iterate through the options
for (i=0; i < options.length; i++) {
    // Query for the current CI
    var ci = new GlideRecord('cmdb_ci_server');
    ci.get(options[i]);

    // Create a new task record for the current CI
    var sct = new GlideRecord('sc_task');
    sct.initialize();
    sct.short_description = 'Task for ' + ci.name;
    sct.assignment_group = ci.u_assignment_group; // Make sure this field name is correct!
    sct.request_item = current.sys_id;
    sct.insert();
}

However, no task is being created. Is it something with the code? Btw, it should work for workflows, not Flows.

3 REPLIES 3

Dawid2
Giga Guru

Okay, it's solved. As advised by @William Coursen, modifying first line to 

var options = current.variables.list_collector.toString().split(',');

did the trick 🙂

Andrew Meza
Tera Expert

Hello @Dawid2 

 

How did you execute this? By chance are you able to provide a screenshot of where you implemented the code?