Declare an Array and its values in Script include and access them based on catalog items field value

Ashwini3
Kilo Contributor

Hi Team,

I have a requirement in catalog development, our client has shared values of a catalog item field as below

Display value                    Yes/No

Abc                                      Yes

xyz                                        No

pqr                                        No

In the drop down field on catalog item fields will be like abc, xyz and pqr. And based on users selection suppose user selects "abc" it corresponding Yes/No field has value "Yes" so catalog task 1 should be created. Similarly if "xyz" is selected then catalog task 1 should not be created. 

Kindly let me how can I achieve this.

How can I set both the above values in a array in script include and access the yes/no field based on value selection on the catalog item field.

Note:

1) We are using single workflow for multiple catalog items. Using script include to access different catalog task short description and assignment group based on catalog item.

2) These values may vary in future so need to hard code at minimum level.

3) These values are around 80

 

Thanks for your valuable inputs.

 

3 REPLIES 3

kklosterman
Giga Guru

Maybe Something like this ?

var variablesToCheck = "Abc,xyz,pqr";

var tasksToCreate = [];

variablesToCheck.split(',').forEach(function(varName){
    if (current.variables[varName] == 'Yes') {
        tasksToCreate.push(varName);
    }
});

// Create Task using a seperate Mapping Table ?

var gr = new GlideRecord('CUSTOM_TABLE_HERE');
gr.addQuery('task_var_name', 'IN', tasksToCreate.join(','));
gr.query();

while ( gr.next() ) {
    // Create SC Task
    var scTask = new GlideRecord('sc_task');
    scTask.initialize();
    scTask.setValue('request_item', current.sys_id);
    scTask.setValue('short_description', gr.getValue('CUSTOM_SHORT_DESC_FIELD_NAME'));
    scTask.setValue('description', gr.getValue('CUSTOM_DESC_FIELD_NAME'));
    scTask.insert();

    // May need to copy Request Variables over to sc_task, can't remember
}

 

OR you can have the var names in the custom table

var gr = new GlideRecord('CUSTOM_TABLE_HERE');
gr.query();

while (gr.next()) {
    if (current.variables[gr.getValue('u_var_name_field_here')] && current.variables[gr.getValue('u_var_name_field_here')] == 'Yes') {

        // Create SC Task
        var scTask = new GlideRecord('sc_task');
        scTask.initialize();
        scTask.setValue('request_item', current.sys_id);
        scTask.setValue('short_description', gr.getValue('CUSTOM_SHORT_DESC_FIELD_NAME'));
        scTask.setValue('description', gr.getValue('CUSTOM_DESC_FIELD_NAME'));
        scTask.insert();

        // May need to copy Request Variables over to sc_task, can't remember

    }
}

Ashwini3
Kilo Contributor

Thanks for your reply but we are not allowed to create custom table in our instance.

As per my approach(could be feasible or non feasible), may be I can save the question choices and its corresponding yes/no in an array and then access the field value selected by user in the script include and traverse through the array to find it and once found send back the Yes/no option to the workflow. Based on it the catalog task will be created or not.

If this is feasible, kindly can you help me with the coding part. As I have not much worked with arrays.

kklosterman
Giga Guru

Could do something like this, but all of the variables you would want to check would have to be hard coded in the script include, since you cannot create a custom table.

 

// Script Include

var CustomScriptInclude = Class.create(); // Name this something relevant 
CustomScriptInclude.prototype = {
    initialize: function () {
        this.variablesToCheck = [
            { "name": "Abc", "short_description": "This is a short Description", "description": "This is a description"}, // can add what ever other variables you want to set in task here
            { "name": "xyz", "short_description": "This is a short Description", "description": "This is a description"},
            { "name": "pqr", "short_description": "This is a short Description", "description": "This is a description"}
        ];
    },

    createTasksFromVariables: function (ritm) {
        var tasksToReturn = [];
        if (ritm) {

            var tasksToCreate = [];

            this.variablesToCheck.forEach(function (variable) {
                if (ritm.variables[variable.name] == 'Yes') {
                    tasksToCreate.push(variable);
                }
            });

            tasksToCreate.forEach(function (varTask) {
                // Create SC Task
                var scTask = new GlideRecord('sc_task');
                scTask.initialize();
                scTask.setValue('request_item', ritm.sys_id);
                scTask.setValue('short_description', varTask.short_description);
                scTask.setValue('description', varTask.description);
                // .. assignement group // .. assigned to ----- All can be stored in the object definition and set here
                scTask.insert();
                // May need to copy Request Variables over to sc_task, can't remember

                tasksToReturn.push(scTask);
            });

        }
        return tasksToReturn;
    },

    type: 'CustomScriptInclude'
};

 

In your workflow call it from a run script whenever you need to like this

 

var csi = new CustomScriptInclude();
// gs.log(csi.createTasksFromVariables(current));
csi.createTasksFromVariables(current);