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.

Getting UI view in business rule

brian_corpuz
Tera Expert

Posting the solution here for future reference.

This comes in handy when the objective is to filter the list result based on the UI view selected.

 

1. Ensure the BR will only be triggered if the session is interactive.

        Condition: gs.isInteractive()

2. Below is the sample code snippet to be placed in the script.

***************************************************************

(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var gr = new GlideRecord('sys_ui_view');
gr.get(RP.getViewID());

 

if ((gs.hasRole('ntg_user') || gs.hasRole('ntg_admin')) && !gs.hasRole('admin')) {
current.addEncodedQuery('category=NTG');
} else if (!gr.name.nil() && gr.name.toString() == 'ntg_view') {
current.addEncodedQuery('category=NTG');
} else {
return;
}
})(current, previous);

*************************************************************** 

2 REPLIES 2

Amit Gujarathi
Giga Sage
Giga Sage

HI @brian_corpuz ,
I trust you are doing great.
To ensure that the business rule (BR) is triggered only when the session is interactive, you can use the following condition:

if (gs.isInteractive()) {
    // Add your code here
}

 

Now, let's incorporate the sample code snippet into the script:

(function executeRule(current, previous /*null when async*/) {
    // Add your code here
    var gr = new GlideRecord('sys_ui_view');
    gr.get(RP.getViewID());

    if ((gs.hasRole('ntg_user') || gs.hasRole('ntg_admin')) && !gs.hasRole('admin')) {
        current.addEncodedQuery('category=NTG');
    } else if (!gr.name.nil() && gr.name.toString() == 'ntg_view') {
        current.addEncodedQuery('category=NTG');
    } else {
        return;
    }
})(current, previous);

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Hi Amit,

 

Appreciate the response.

 

I think it is better to use the condition field to specify the gs.isInteractive()