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.

Flow Designer Script Check Department Membership

darronf
Tera Expert

I'm struggling with some script in a Flow.  I'm using the Set Flow Variables Flow Logic.  If I utilize the first part of the script and simply return deptID, I get "ITO" as expected.  However, when I try to verify that the deptID that comes in from the Catalog Variables is one of the options listed in the deptList array, it returns "false" every time.  What am I missing?

 

var deptList = ["CLM", "CRQ", "CRS", "CRU", "ITD", "ITO", "ITS", "OPF"];
var deptID = fd_data.trigger.request_item.variables.dept;
var containsDept = false;

for (var i = 0; i < deptList.length; i++) {
    if (deptID.includes(deptList[i])) {
        containsDept = true;
        break;
    }
}

return containsDept;

 

1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@darronf Try converting the dept variable to string (if it is not a reference variable )and see if the match works

 

var deptList = ["CLM", "CRQ", "CRS", "CRU", "ITD", "ITO", "ITS", "OPF"];
var deptID = fd_data.trigger.request_item.variables.dept.toString();
var containsDept = false;

for (var i = 0; i < deptList.length; i++) {
    if (deptID.includes(deptList[i])) {
        containsDept = true;
        break;
    }
}

return containsDept;

If dept is a reference variable then you should try the following.

var deptList = ["CLM", "CRQ", "CRS", "CRU", "ITD", "ITO", "ITS", "OPF"];
var deptID = fd_data.trigger.request_item.variables.dept.name.toString(); //if dept is a reference variable
var containsDept = false;

for (var i = 0; i < deptList.length; i++) {
    if (deptID.includes(deptList[i])) {
        containsDept = true;
        break;
    }
}

return containsDept;

Hope this helps.

View solution in original post

4 REPLIES 4

Saloni Suthar
Giga Sage
Giga Sage

Hi @darronf ,

You must be getting the sys_id of the department from your variable if it is a reference variable. If that is the case, you have to do :

var deptID = fd_data.trigger.request_item.variables.dept.name;

 

 


If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni

Thanks for your reply.  I was needing to convert the variable to a string.  Appreciate your help!

Sandeep Rajput
Tera Patron
Tera Patron

@darronf Try converting the dept variable to string (if it is not a reference variable )and see if the match works

 

var deptList = ["CLM", "CRQ", "CRS", "CRU", "ITD", "ITO", "ITS", "OPF"];
var deptID = fd_data.trigger.request_item.variables.dept.toString();
var containsDept = false;

for (var i = 0; i < deptList.length; i++) {
    if (deptID.includes(deptList[i])) {
        containsDept = true;
        break;
    }
}

return containsDept;

If dept is a reference variable then you should try the following.

var deptList = ["CLM", "CRQ", "CRS", "CRU", "ITD", "ITO", "ITS", "OPF"];
var deptID = fd_data.trigger.request_item.variables.dept.name.toString(); //if dept is a reference variable
var containsDept = false;

for (var i = 0; i < deptList.length; i++) {
    if (deptID.includes(deptList[i])) {
        containsDept = true;
        break;
    }
}

return containsDept;

Hope this helps.

Thanks so much for your help!  toString() did the trick.