Encountered undeclared output variable error in Script action - Flow designer

Mahalakshmi Rav
Tera Contributor

I'm working on a Flow Designer Action where I need to filter users based on the itil role using a Script Include and return a list of their sys_ids via Script step output variable.

 

Script Include (GetITILValidators):

 

var GetITILValidators = Class.create();
GetITILValidators.prototype = {
initialize: function() {},

getITILUsers: function(validatorsList) {
var itilUsers = [];

if (!validatorsList)
return itilUsers;

var userIds = validatorsList.split(',');

for (var i = 0; i < userIds.length; i++) {
var user = new GlideRecord('sys_user');
if (user.get(userIds[i]) && user.roles.toString().includes('itil')) {
itilUsers.push(user.sys_id.toString());
}
}

return itilUsers;
},

type: 'GetITILValidators'
};

 

Flow Designer Action:
Input: user_ids (Type: String, Mandatory)

MahalakshmiRav_0-1754342263443.png

 

Output: filtered_users (Type: Array.String)

MahalakshmiRav_1-1754342278563.png

Script Step:

(function execute(inputs, outputs) {
    var helper = new GetITILValidators();
    var result = helper.getITILUsers(inputs.user_ids);
    outputs.filtered_users = result;
})(inputs, outputs);
 
MahalakshmiRav_2-1754342351016.png
Whenever I run a Test in the Action Designer using any valid user sys_id (with itil role) the test fails with this error
Warning: Encountered undeclared output variable: filtered_users

 

MahalakshmiRav_3-1754342462748.png

What adjustment do I need to make in the code or Flow Action setup to fix this?

Any help would be appreciated!

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Mahalakshmi Rav 

did you create an input and output variable under the Script Step?

You need to create it with the correct type based on what script include returns and based on what the action is accepting as input.

Then map the Action Output with Script Step Output

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Shruti
Mega Sage
Mega Sage

Hi,

Create Input variable and output variables in the Script step of the flow action

Shruti_0-1754371106658.png

Shruti_1-1754371214543.png

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Mahalakshmi Rav 

did you create an input and output variable under the Script Step?

You need to create it with the correct type based on what script include returns and based on what the action is accepting as input.

Then map the Action Output with Script Step Output

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar , Thank you, it worked. 

I have a requirement to create a Change Task only if the validators(list var from change request)  have the ITIL role and it should be automatically assigned to them.

What I tried:

  • I used a Script Include to check whether users have the ITIL role.

  • I called this Script Include inside a Script Action.

  • Based on the Script Action’s output variable, the Main Flow is supposed to create a Change Task and assign it to the filtered users.

 

My Script include:

var GetITILValidators = Class.create();
GetITILValidators.prototype = {
    initialize: function () {
    },

    getITILUsers: function (userIds) {
        var filtered = [];

        if (!userIds) {
            return filtered;
        }

        var ids = userIds.split(',');

        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('sys_id', 'IN', ids);
        userGR.query();

        while (userGR.next()) {
            var hasRole = new GlideRecord('sys_user_has_role');
            hasRole.addQuery('user', userGR.sys_id);
            hasRole.addQuery('role.name', 'itil');
            hasRole.query();

            if (hasRole.hasNext()) {
                filtered.push(userGR.sys_id.toString());
            }
        }

        return filtered;
    },

    type: 'GetITILValidators'
};

My Script action:

MahalakshmiRav_1-1754570489811.png

 

MahalakshmiRav_2-1754570509354.png

 

MahalakshmiRav_3-1754570523287.png

I ran a test for the script action by giving the user_ids (i.e) one user id is ITIL and other is Non-ITIL

 
MahalakshmiRav_0-1754570472252.png

 
It's giving the right output values.

MahalakshmiRav_4-1754570617717.png
MahalakshmiRav_5-1754570802619.png

 

Main flow:

MahalakshmiRav_6-1754571108687.png

 

MahalakshmiRav_7-1754571122900.png

 

MahalakshmiRav_8-1754571139307.png

When I ran a test for this main flow, the "Create Task" step didn’t get triggered. This is because the output variable is empty and it’s not passing the ITIL users.

I created the change request in the backend and provided the same validators that I used in the Script Action test.

Can you please tell me what went wrong here?

MahalakshmiRav_9-1754571359334.png

 

 

 

 

@Mahalakshmi Rav 

I could not see output variable declared for your Script Step.

-> Did you create it of type Array?

-> the name of that output variable should be filtered_users

-> Then the Action Output will be assigned with the Output variable from Script Step (which you just now created)

-> Once you do then the array will come properly with the output from Script Step

AnkurBawiskar_0-1754573280423.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader