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

That could work, though it would be harder to incorporate the conditional addQuery if the sequence is not blank.  Manually filter a list view on your table by the criteria, selecting the  cat_item and sequence number used in your test case to see how many rows are returned, then tweak it to return the expected rows.  If the query should return multiple records but you only want to create one Catalog Task record, you can change the while to if. 

Community Alums
Not applicable

You have been so helpful. Thank you so much. I realized that I could just use setLimit() on the query. I will have to test it several times b/c the event occurs ever now and then. But I will definitely take your suggestion.