Workflow RunScript to Insert Catalog tasks

Brett21
Tera Guru

Hi Community,

 

I took this script from the community forum but I am having issues with it.  I have a list collector variable that a user can select multiple CIs. The run script takes each CI and creates a separate catalog task.  The issue is if two CIs are selected and two catalog tasks are selected only one of the catalog tasks will display the variables.  Overall, I am looking to see if there is another run script out there to use(I couldn't find one) that will display the variables on all catalog tasks generated.  I will paste the run script in this post so that the community can take a look. 

 

createCatalogTasks();

function createCatalogTasks() {
    var list = current.variables.idServerName.toString();
    var arrayList = list.split(",");
    for (var idx = 0; idx < arrayList.length; idx++) {
        // Query for the current CI
        var ci = new GlideRecord('cmdb_ci');

        if (ci.get(arrayList[idx])) {

            var task = new GlideRecord('sc_task');

            task.initialize();

            task.request_item = current.sys_id;

            task.requested_for = current.requested_for;

            task.short_description = 'Perform De-Install Protocol Task for ' + ci.name;

            task.assignment_group = ci.support_group;

            task.parent = current.sys_id;

            //task.state = 'OPEN'; //state is open

            task.insert();

            //insert one variable on task form

            //             var myVar = new GlideRecord('sc_item_variables_task');

            //             myVar.initialize();

            //             myVar.task = task.sys_id;

            //             myVar.variable = 'kdjvndv23894u29385'; //sys_id for the vairable from item_option_new table

            //             myVar.insert();

            //end of inserting one variable
			

            //insert all catalog item variables on task form

            var catVariables = new GlideRecord('item_option_new');

            catVariables.addQuery('cat_item', current.cat_item);
			
			catVariables.addEncodedQuery('name!=spIPaddressgateway');

            catVariables.query();

            while (catVariables.next()) {

                var myVar = new GlideRecord('sc_item_variables_task');

                myVar.initialize();

                myVar.task = task.sys_id;

                myVar.variable = catVariables.sys_id;

                myVar.insert();
            }
            //end of inserting all catalog item variables on task form
        }

    }
    //start - insert all catalog item variable-set variables on task form 


    var variableSets = new GlideRecord('io_set_item');


    workflow.info('current.cat_item = ' + current.cat_item);


    variableSets.addQuery('sc_cat_item', current.cat_item);


    variableSets.query(); // looking for io_set_item.variable_set == item_option_new_set.sys_id




    while (variableSets.next()) { // iterate through variable sets on catalog item


        var setVariables = new GlideRecord('item_option_new_set');


        workflow.info('variableSets.variable_set = ' + variableSets.variable_set);


        setVariables.addQuery('sys_id', variableSets.variable_set);


        setVariables.query();



        while (setVariables.next()) { // iterate through each variable set


            var catsetVariables = new GlideRecord('item_option_new');


            workflow.info('setVariables.sys_id = ' + setVariables.sys_id);


            catsetVariables.addQuery('variable_set', setVariables.sys_id);


            catsetVariables.addQuery('type', 'NOT IN', '280d44163720300054b6a3549dbe5d3c,ec0d44163720300054b6a3549dbe5d3c,311dc651c3121100c8b837659bba8fc4,ac0d44163720300054b6a3549dbe5d3c');


            // Type is NOT (Break, Container End, Container Split, Container, Start)


            catsetVariables.query();



            while (catsetVariables.next()) { // iterate through variables in variable set


                var mysetVar = new GlideRecord('sc_item_variables_task');


                mysetVar.initialize();


                workflow.info('task.sys_id = ' + task.sys_id);


                mysetVar.task = task.sys_id;


                workflow.info('catsetVariables.sys_id = ' + catsetVariables.sys_id);


                mysetVar.variable = catsetVariables.sys_id;


                mysetVar.insert();
            }
        }
    }
    //end - insert all catalog item variable-set variables on task form
}

 

1 ACCEPTED SOLUTION

Brett21
Tera Guru

I found the issue with my run script. The code for inserting variable set variables was outside the loop that creates the tasks. Once I made the correction testing was successful. This is for anyone that runs across this post.

Updated code below

createCatalogTasks();

function createCatalogTasks() {
    var list = current.variables.idServerName.toString();
    var arrayList = list.split(",");

    for (var idx = 0; idx < arrayList.length; idx++) {
        // Query for the current CI
        var ci = new GlideRecord('cmdb_ci');

        if (ci.get(arrayList[idx])) {

            var task = new GlideRecord('sc_task');

            task.initialize();
            task.request_item = current.sys_id;
            task.requested_for = current.requested_for;
            task.short_description = 'Perform De-Install Protocol Task for ' + ci.name;
            task.assignment_group = ci.support_group;
            task.parent = current.sys_id;
            task.insert();

            // Insert all catalog item variables on task form
            var catVariables = new GlideRecord('item_option_new');
            catVariables.addQuery('cat_item', current.cat_item);
            catVariables.addEncodedQuery('name!=spIPaddressgateway');
            catVariables.query();

            while (catVariables.next()) {
                var myVar = new GlideRecord('sc_item_variables_task');
                myVar.initialize();
                myVar.task = task.sys_id;
                myVar.variable = catVariables.sys_id;
                myVar.insert();
            }

            // Insert variable set variables inside the task loop**
            var variableSets = new GlideRecord('io_set_item');
            variableSets.addQuery('sc_cat_item', current.cat_item);
            variableSets.query();

            while (variableSets.next()) {
                var setVariables = new GlideRecord('item_option_new_set');
                setVariables.addQuery('sys_id', variableSets.variable_set);
                setVariables.query();

                while (setVariables.next()) {
                    var catsetVariables = new GlideRecord('item_option_new');
                    catsetVariables.addQuery('variable_set', setVariables.sys_id);
                    catsetVariables.addQuery('type', 'NOT IN', '280d44163720300054b6a3549dbe5d3c,ec0d44163720300054b6a3549dbe5d3c,311dc651c3121100c8b837659bba8fc4,ac0d44163720300054b6a3549dbe5d3c');
                    catsetVariables.query();

                    while (catsetVariables.next()) {
                        var mysetVar = new GlideRecord('sc_item_variables_task');
                        mysetVar.initialize();
                        mysetVar.task = task.sys_id; // Correctly referencing the task inside the loop
                        mysetVar.variable = catsetVariables.sys_id;
                        mysetVar.insert();
                    }
                }
            }
        }
    }
}

View solution in original post

2 REPLIES 2

Brett21
Tera Guru

I think I narrowed it down to the variable sets section of the script.  Another pair of eyes would be awesome thank you to any that can check this out.

Brett21
Tera Guru

I found the issue with my run script. The code for inserting variable set variables was outside the loop that creates the tasks. Once I made the correction testing was successful. This is for anyone that runs across this post.

Updated code below

createCatalogTasks();

function createCatalogTasks() {
    var list = current.variables.idServerName.toString();
    var arrayList = list.split(",");

    for (var idx = 0; idx < arrayList.length; idx++) {
        // Query for the current CI
        var ci = new GlideRecord('cmdb_ci');

        if (ci.get(arrayList[idx])) {

            var task = new GlideRecord('sc_task');

            task.initialize();
            task.request_item = current.sys_id;
            task.requested_for = current.requested_for;
            task.short_description = 'Perform De-Install Protocol Task for ' + ci.name;
            task.assignment_group = ci.support_group;
            task.parent = current.sys_id;
            task.insert();

            // Insert all catalog item variables on task form
            var catVariables = new GlideRecord('item_option_new');
            catVariables.addQuery('cat_item', current.cat_item);
            catVariables.addEncodedQuery('name!=spIPaddressgateway');
            catVariables.query();

            while (catVariables.next()) {
                var myVar = new GlideRecord('sc_item_variables_task');
                myVar.initialize();
                myVar.task = task.sys_id;
                myVar.variable = catVariables.sys_id;
                myVar.insert();
            }

            // Insert variable set variables inside the task loop**
            var variableSets = new GlideRecord('io_set_item');
            variableSets.addQuery('sc_cat_item', current.cat_item);
            variableSets.query();

            while (variableSets.next()) {
                var setVariables = new GlideRecord('item_option_new_set');
                setVariables.addQuery('sys_id', variableSets.variable_set);
                setVariables.query();

                while (setVariables.next()) {
                    var catsetVariables = new GlideRecord('item_option_new');
                    catsetVariables.addQuery('variable_set', setVariables.sys_id);
                    catsetVariables.addQuery('type', 'NOT IN', '280d44163720300054b6a3549dbe5d3c,ec0d44163720300054b6a3549dbe5d3c,311dc651c3121100c8b837659bba8fc4,ac0d44163720300054b6a3549dbe5d3c');
                    catsetVariables.query();

                    while (catsetVariables.next()) {
                        var mysetVar = new GlideRecord('sc_item_variables_task');
                        mysetVar.initialize();
                        mysetVar.task = task.sys_id; // Correctly referencing the task inside the loop
                        mysetVar.variable = catsetVariables.sys_id;
                        mysetVar.insert();
                    }
                }
            }
        }
    }
}