Workflow is generating more than one sc task - help?

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();
    }
}