The CreatorCon Call for Content is officially open! Get started here.

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

@Mahalakshmi Rav 

you didn't update the script include correctly.

Line 21 should use userIds and not ids

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

Thank you @Ankur Bawiskar , the solution worked but I’m facing two issues

  1. Out of the 2 ITIL users, only one task is getting created. Ideally 2 tasks should be created based on the ITIL Validators count and each task should be assigned to the respective filtered user. For eg, if there are 3 ITIL users (in the validators field) in the change request, then 3 ctasks should be created and assigned to each filtered users

    MahalakshmiRav_4-1755798065584.pngMahalakshmiRav_5-1755798078984.png
  2. I’m trying to set the Change Task assignee based on the filtered users, but the filtered users field is greyed out in the main Flow, so I’m unable to map the value

     

    MahalakshmiRav_6-1755798268196.png

     

@Mahalakshmi Rav 

Glad to know that my solution worked.

Would you mind marking my response as correct as I provided solution to your original question?

The discussion can continue on answered thread as well.

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

@Mahalakshmi Rav 

see this where I shared solution on how to get and iterate. I used JSON structure and you can use the same

flow action iterate array of objects in subflow.gif

Also attaching the gif here

also check below link on how to do it in another way

How to iterate an Array.String in the flow designer?  -> response from Hitoshi

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

@Mahalakshmi Rav 

Thank you for marking my response as helpful.

As per new community feature you can mark multiple responses as correct.

If my response helped please mark it correct as well so that it benefits future readers.

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