Workflow is generating more than one sc task - help?

Community Alums
Not applicable

Hey All, 

 

I hope all is well. I'm trying to troubleshoot this workflow that I didn't build. It is generating multiple tasks randomly. It should only generate one. I read that loops can cause this issue. I removed the loop and I'm still having issues. What am I missing? Any suggestions? 

 

var taskID = []; //Array to hold created tasks

// Query the Fulfillment table
var gr_Fulfillment = new GlideRecord('u_catalog_workflow_activities');
gr_Fulfillment.addQuery('u_catalog_item', current.cat_item + '');
gr_Fulfillment.addQuery('u_type', 'Catalog Task');

if (!gs.nil(workflow.scratchpad.fulfillment_sequence))
    gr_Fulfillment.addQuery('u_sequence', Number(workflow.scratchpad.fulfillment_sequence));
else
    gr_Fulfillment.orderBy('u_sequence');

gr_Fulfillment.query();

while (gr_Fulfillment.next()) {
    if (gs.nil(workflow.scratchpad.fulfillment_sequence))
        workflow.scratchpad.fulfillment_sequence = Number(gr_Fulfillment.u_sequence);

    // Create the catalog item tasks
    var gr_NewTask = new GlideRecord('sc_task');
    gr_NewTask.newRecord();

    // Check if there is a template
    if (!gs.nil(gr_Fulfillment.u_task_template)) {
        // Apply the template
        gr_NewTask.applyTemplate(gr_Fulfillment.u_task_template.name);
    }

    //Set Catalog Task Short Description and Description
    if (!gs.nil(gr_Fulfillment.u_description))
        gr_NewTask.description = gr_Fulfillment.u_description;
    if (gs.nil(gr_NewTask.short_description) && !gs.nil(gr_Fulfillment.u_short_description)) {
        gr_NewTask.short_description = gr_Fulfillment.u_short_description;
    } else {
        gr_NewTask.short_description = 'Please fulfill ' + current.cat_item.name + ' to the requested for user.';
    }

    // Associate the new catalog task to the RITM
    gr_NewTask.request = current.request + '';
    gr_NewTask.request_item = current.sys_id + '';
    gr_NewTask.parent = current.sys_id + '';

    if (gs.nil(gr_NewTask.assignment_group) && !gs.nil(gr_Fulfillment.u_assignment_group))
        gr_NewTask.assignment_group = gr_Fulfillment.u_assignment_group;

    gr_NewTask.insert();
	
    taskID.push(gr_NewTask.sys_id + ''); //Add new SCTASK to array
}

//----------Add Variables to SCTASK Variable Editor----------

var varSet = ''; //Create Variable Set string

//Iterate through each created task
for (var i = 0; i < taskID.length; i++) {

	//Query for Variable Sets on the Catalog Item
    var setCheck = new GlideRecord('io_set_item');
    setCheck.addQuery('sc_cat_item', current.cat_item);
    setCheck.query();

    while (setCheck.next()) {
        varSet += '^ORvariable_set=' + setCheck.variable_set; //Add variable set to query
    }

	//Query for Variables on the Catalog Item + on the associated Variable Sets
    var getVar = new GlideRecord('item_option_new');
    getVar.addEncodedQuery('cat_item=' + current.cat_item + varSet);
    getVar.query();

    while (getVar.next()) {

		//Add variable to SC_TASK Variable Editor
        var setVar = new GlideRecord('sc_item_variables_task');
        setVar.initialize();
        setVar.task = taskID[i];
        setVar.variable = getVar.getValue('sys_id');
        setVar.update();
    }
}

 

 

 

6 REPLIES 6

Brad Bowman
Kilo Patron
Kilo Patron

As far as the script itself, each time it runs it is going to create one sc_task record for each record in your u_catalog_workflow_activities table retrieved.  You could add a log to the script if you are uncertain how many records the GlideRecord is retrieving.  If the workflow is where you removed a loop, have you confirmed that there is now only one path to this Run Script activity, and are you testing with a newly-created record?  Existing requests will continue to use the workflow as it existed when the request was created.

Community Alums
Not applicable

What is happening is when we create a Service Catalog request from the workspace, it generates multiple tasks. I initially thought it was the UI action, but when I removed this workflow with a flow designer flow, we didn't have an issue. The problem is that this workflow is used for multiple catalog items like over 70 plus items. We need to fix it. 

 

 

I did. I refreshed and created a new interaction, but same issue. 

Do you know how many records the GlideRecord on your fulfillment table is retrieving in your test case?

Community Alums
Not applicable

Actually, it is at the Fulfillment table is retrieving more than one record. Maybe I should use an encoded query instead?