Run script is not working as intended

SusanSchwar
Tera Contributor

I am creating a run script for a fulfiller multi-row variable set within a legacy workflow for a catalog item.  This is how I want the run script to work:

When the request is submitted, an initial task should automatically generate with the fulfiller multi-row-variable set and it should be assigned to a specific fulfiller user. The fulfiller user will fill in the fulfiller multi-row variable set. For each row on the multi-row variable set, a task should generate. Each row will contain a field with the assignment group and assigned to user for that task. Once every row has a task, then the script is done.

When I tested my script, the ritm is set to closed complete with no task generated. I'm not sure why it's not working as intended.



 //This script is to generate a task per row on a fulfiller MRVS. The assignment group and assigned to user are pulled from the fields on each row. 

 var mrvs = current.variables.fulfiller_task_triage;
 var rowCount = mrvs.getRowCount();
 //Fulfiller initial triage task. 
 var ft = new GlideRecord("sc_task");
 //Create the SCtask
 var gr = new GlideRecord("sc_task");

 ft.initialize();

 ft.task.short_description = "Task Triage";
 ft.task.assignment_group = "fulfiller user group";
 ft.task.assigned_to = "fulfiller user";
//Once the task has been completed and submitted by the fulfiller, the for loop wil; get the rows from the intial task and generate a task for each row in MRVS
 for (var i = 0; i < rowCount; i++) {
     //Initialize the sctask
     gr.initialize();
     var row = mrvs.getRow(i);
     var newRow = mrvs.addRow();
     var triageSelect = current.variable.choose_a_triage_selection;
     gr.state = 1;
     var triageSelection = rows[i].choose_a_triage_selection || '';
     if (triageSelection.indexOf('option 1') > -1) {
         scTask.short_description =
             current.number + ' - ' +
             current.variables.request_title + ' - option1';
     } else if (triageSelection.indexOf('option 2') > -1) {
         scTask.short_description =
             current.number + ' - ' +
             current.variables.request_title + ' - option 2';
     } else if (triageSelection.indexOf('option 3') > -1) {
         scTask.short_description =
             current.number + ' - ' +
             current.variables.request_title + ' -option 3';
     }

     gr.assignment_group = current.variables.select_the_assignment_group_for_this_task;
     gr.assigned_to = current.variables.select_the_assigned_to_user_for_the_task;

 }

 gr.insert();

 

2 REPLIES 2

Sachin_Nasa
Kilo Sage

Hi @SusanSchwar ,

I noticed a few issues in your existing script that might be causing the problem.

  1. You are initializing the initial task using ft.initialize(), but you are not inserting it using ft.insert().

  2. You are using rows[i], but rows is not declared. You already have row = mrvs.getRow(i), so you can use row directly.

  3. You are using scTask.short_description, but your GlideRecord variable is named gr.

  4. You are calling mrvs.addRow(), which is unnecessary because you are trying to retrieve existing MRVS rows.

  5. Most importantly, gr.insert() is outside the for loop. Move it inside the loop so that a separate task is created for every MRVS row.

  6. You are retrieving the assignment group and assigned-to values from current.variables instead of the corresponding fields in each MRVS row.

You can try correcting the existing script as follows:

var mrvs = current.variables.fulfiller_task_triage;
var rowCount = mrvs.getRowCount();

for (var i = 0; i < rowCount; i++) {

    var row = mrvs.getRow(i);

    var gr = new GlideRecord('sc_task');
    gr.initialize();

    gr.setValue('request_item', current.getUniqueValue());

    var triageSelection = row.choose_a_triage_selection.toString();

    if (triageSelection.indexOf('option 1') > -1) {
        gr.setValue('short_description',
            current.number + ' - ' +
            current.variables.request_title + ' - option 1');

    } else if (triageSelection.indexOf('option 2') > -1) {
        gr.setValue('short_description',
            current.number + ' - ' +
            current.variables.request_title + ' - option 2');

    } else if (triageSelection.indexOf('option 3') > -1) {
        gr.setValue('short_description',
            current.number + ' - ' +
            current.variables.request_title + ' - option 3');
    }

    gr.setValue('assignment_group',
        row.select_the_assignment_group_for_this_task);

    gr.setValue('assigned_to',
        row.select_the_assigned_to_user_for_the_task);

    gr.setValue('state', 1);

    gr.insert();
}

Also, regarding the RITM getting closed complete, check whether your workflow is reaching the End activity without waiting for the initial triage task to be completed.

One more thing to verify: make sure the fulfiller's MRVS responses are saved against the RITM before this Run Script executes. Creating an sc_task does not automatically make the RITM's MRVS editable on the task form.

I would first test whether mrvs.getRowCount() returns the expected number of rows and then check whether the tasks are being inserted successfully.

Reference Docs: https://www.servicenow.com/docs/r/servicenow-platform/service-catalog/c_ScriptableServiceCatalogVari...

If you found this helpful, please mark it as Helpful and Accept as Solution so it can reach others too.

jeffreyluto
Tera Contributor

I constantly mess up syntax.

Try a JS validator

.