Question about .includes()

CP-UW
Tera Contributor

An external developer wrote a script to find UI Actions that don't have "gs.hasRole" in the condition field.

Their code looks like this:

    if (JSUtil.notNil(current.condition)) {
        if (current.condition.includes("gs.hasRole")) {
            found = false;
        }
    }

Somehow this is not just looking at the string contents of the field, it's also looking at the scripts a UI action is calling and seeing if "gs.hasRole" is in there.

 

When I try doing this with a GlideRecord item, it does not work the same way.

 

if (grActions.condition.includes("gs.hasRole")) {
     found = false;
 }

 

Trying to search for what this .includes() is doing is impossible, because the only thing that comes up is script includes, which is not what is happening here.

 

If someone could point me in the right direction on how this nested search is working, that would be great.

4 REPLIES 4

LJ86
Kilo Guru

.includes() is a standard String method in JS, nothing ServiceNow specific - in this use case, it would simply read the Condition field as plain text - it can't look into other scripts called there as such calls would only be interpreted at runtime of any particular UI action, not when this script runs. 

Why do you believe the first script performs a "nested search"? The answer may be somewhere else in the code, as the bit you've pasted is actually to do with conditions that do contain "gs.hasRole"...

 

Reference for the .includes() method:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes

CP-UW
Tera Contributor

The reason I think theirs is drilling down is because I have a UI action that does not have "gs.hasRole" in its condition, but it calls an included script and that script does have "gs.hasRole". When I use this exact code, I get what you'd expect, it parses the string contents of condition, looks for that string and returns false.


Their scan returns ~190 records. When I use this same code, I get ~1550 rows. I am not sure what the origin of the discrepancy is. I don't think in this case, it's ultimately going to affect what I do (adding a base level role to all of the identified records), but I'd still like to understand why it's happening.


This is their whole code. Their base table for their query is sys_ui_action and their only parameter is active=true.

(function(finding, current) {

    var found = true;
    var gr = new GlideRecord("sys_ui_action_view");
    gr.addQuery('sys_ui_action', current.sys_id);
    gr.query();
    if (gr.next()) {
        found = false;
    }
    if (JSUtil.notNil(current.condition)) {
        if (current.condition.includes("gs.hasRole")) {
            found = false;
        }
    }

    var gr2 = new GlideRecord('sys_ui_action_role');
    gr2.addQuery('sys_ui_action', current.sys_id);
    gr2.query();
    if (gr2.next()) {
        found = false;
    }
    if (found) {
        finding.setCurrentSource(current);
        finding.increment();
    }

})(finding, current);

Is it not because the UI action in question has a Required Role and/or Visibility record linked to it, like in the examples below?

 

LukaszJ_0-1720085455328.png

LukaszJ_1-1720086167656.png

 

This is what the other parts of the script are checking and I would expect this is why it returns false.

CP-UW
Tera Contributor

No, they have no roles, no views, no condition associated with them.