MRV to Create a new Catalog task for each row

Sam Ogden
Tera Guru

Hi All,

We have a catalog item which contains a tasks of 'triage'.  The user then needs to identify all areas of the business that need to do an impact assessment for the item.  I was thinking of using a Multi Row Variable set which would contain 2 columns (assignment group & details) on the catalog task so the user doing the triage can add in each area that needs to do an impact assessment.
When the user closes the triage task I would want a catalog task to be created for each row in the MRV with the assignment group variable setting the assignment group and the details variable being the description field?

Any help on this would be greatly appreciated.

Thanks

Sam

6 REPLIES 6

DrewW
Mega Sage
Mega Sage

I have done something like this and basically what I did was use a script action to create all of the catalog tasks then used a wait for action to check to see if there are any open cat tasks for the item and when they were all closed I would then move on.

Hi Drew,

Given this a go with a script and got it working.  Still learning Javascript so could you possibly take a look and let me know if I can do this any better? 

var mrvs = current.variables.cim_triage;
var rowCount = mrvs.getRowCount();
var catTask = new GlideRecord('sc_task');
for (var i=0; i < rowCount; i++) {
	var row = mrvs.getRow(i);
	var group = row.assignment_group;
	var details = row.details;
	
	catTask.initialize();
	catTask.short_description = 'IA Task';
	catTask.description = details;
	catTask.assignment_group = group;
	catTask.request_item = current.getUniqueValue();
	catTask.insert();
}

The only thing I would do is move the var catTask line to just above the initialize just so its all together.  Not really needed just my preference.

Otherwise if it works go with it.  🙂

James Gragston
Tera Guru

samogden,

So, theres a lot of moving parts on this one.

You'll need to figure out a way to create as may tasks as there are rows in the MRVS. To do this, you'll need to grab the number of JSON objects in the MRVS array and create a task for each. When you create each task you'll need to assign its request_item field to the current RITMs sys_id. Otherwise the task won't be tied to the RITM when its generated. Something like this...

generateTask: function () {
    var tasks = new GlideRecord('sc_task');
    tasks.initialize();
    tasks.short_description = 'New task';
    tasks.description = 'new task generated from Demo workflow';
    tasks.request_item = current.sys_id; //tie to current RITM object
    tasks.insert();
},

*the full code is shown below*

 

The current object is the RITM because I'm running a script include from a Run Script workflow activity and the workflow is running on the [sc_req_item] table:

var generator = new TaskGenerator();
generator.generateTask(); //called from workflow

 

So, every time the workflow runs, a new task is generated. This needs to be done for each of the JSON objects (MRVS rows). I've tested this code and it creates tasks for each MRVS you add and associates those tasks to the current RITM:

 

var TaskGenerator = Class.create();
TaskGenerator.prototype = {
    initialize: function () {},
    generateTask: function (current) { //current object in the RITM
        var mrvArrVals = this._getValues(current); //returns 2d array of MRVS values!

        for (var i = 0; i < mrvArrVals.length; i++) {
            var name = mrvArrVals[i][0];
            var email = mrvArrVals[i][1];
            var tasks = new GlideRecord('sc_task');
            tasks.initialize();
            tasks.short_description = name;
            tasks.description = email;
            tasks.request_item = current.sys_id;
            tasks.insert();
        }

    },
    _getValues: function (ritm) {

        var mrvs;
        var valueArr = [];
        var valueArrays = [];
        var sets = new GlideRecord('item_option_new_set');
        sets.addQuery('title', 'Demo MRVS');
        sets.setLimit(1);
        sets.query();

        if (sets.next()) {
            mrvs = sets.internal_name;
        }

        if (ritm) {
            var m = ritm.variables[mrvs];
            var mrvParsed = JSON.parse(ritm.variables[mrvs]);
            var setCount = mrvParsed.length;
            var keys = Object.keys(mrvParsed[0]) + '';
            var keysArr = keys.split(',');

            //iterate through obj
            for (var i = 0; i < setCount; i++) {
                //iterate through keys
                for (var j = 0; j < keysArr.length; j++) {
                    //check if user entered value
                    if (keysArr[j]) {
                        valueArr.push(mrvParsed[i][keysArr[j]]);

                    } else {
                        valueArr.push("no data entered");
                    }
                }
                valueArrays.push(valueArr);
                valueArr = []; //clear the array to repopulate it with next object values
            }
        }
        return valueArrays;
    },

    type: 'TaskGenerator'
};

The _getValues() is simply a helper function w/in my script include that passes a 2D array that holds all of the mrvs values. Since it's an array of arrays, I don't only have my mrvs values, I have the number of mrvs rows! This tells me how many times I need to iterate and create my tasks...Like I said, a lot of moving parts.

 

The below screen shots are the workflow, the MRVS on the catalog item and the result on the RITM record..

 

find_real_file.png

find_real_file.png

find_real_file.png

 

If you have any questions about the code, let me know. Hope this helped!