Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Main Flow not creating Change Task even though Script Action works

Mahalakshmi Rav
Tera Contributor

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_5-1755779301427.png

 

MahalakshmiRav_1-1755779010802.png

 

MahalakshmiRav_2-1755779035560.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_3-1755779247662.png


It's giving the right output values.

 

MahalakshmiRav_4-1755779273248.png

 

 

Main flow:

MahalakshmiRav_6-1755779378866.png

 

MahalakshmiRav_7-1755779399932.png

 

 

MahalakshmiRav_8-1755779426450.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.

MahalakshmiRav_9-1755780512610.png

 

MahalakshmiRav_10-1755780541919.png

 

Can you please tell me what went wrong here?

 

Thank you

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Mahalakshmi Rav 

when you call from main flow it's not treating the input correctly

Do this

1) make input user_ids as Array.String instead of String

2) then use this in script step i.e. convert array to string value and then pass to script include function

getITILUsers(inputs.user_ids.toString())

3) Update script include as this -> no need to split, directly use the value coming to function

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

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

        if (!userIds) {
            return filtered;
        }

        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('sys_id', 'IN', userIds);
        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'
};

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

Fabian Kunzke
Kilo Sage
Kilo Sage

Hey,

 

i think this is due to your input type of the action. Just to confirm, could you - in your main flow - instead of using a data pill to assign the input value, use a script for the value retrival? So instead of providing a data-pill for the "user_ids" instead add a script (with the button right next to it). In that script, just add a .getValue('whatever_the_field_name_of_the_validators_is');

 

Your input variable configuration of your action is a string. However, the field you are using in the data pill seems to be an array. When using data-pills, field types are kept and not converted to simple types. This could be the issue here.

 

Hope this helps,

Regards

Fabian

 

 

Thanks @Fabian Kunzke , I tried using the script option with current.getValue('u_validators'); in my main flow, but it’s throwing an error:

{ "Action Status": { "code": 1, "message": "Error: Cannot convert null to an object.,Detail: Cannot convert null to an object." } }

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Mahalakshmi Rav 

when you call from main flow it's not treating the input correctly

Do this

1) make input user_ids as Array.String instead of String

2) then use this in script step i.e. convert array to string value and then pass to script include function

getITILUsers(inputs.user_ids.toString())

3) Update script include as this -> no need to split, directly use the value coming to function

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

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

        if (!userIds) {
            return filtered;
        }

        var userGR = new GlideRecord('sys_user');
        userGR.addQuery('sys_id', 'IN', userIds);
        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'
};

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 ,

 

Thanks for your response, I tried as per your suggestion but it's throwing the "Error: "ids" is not defined.,Detail: "ids" is not defined. error.

MahalakshmiRav_3-1755784749875.png

 

MahalakshmiRav_0-1755784645940.png

 

MahalakshmiRav_1-1755784657691.png

 

MahalakshmiRav_2-1755784675437.png

MahalakshmiRav_4-1755784859102.png