how to set a condition for UI Action

Ricardo Sanche3
Tera Contributor

gs.hasRole('itil') && newGlideRecord("sys_user").get(gs.getUserID()).company.u_business_unit.getValue().includes('Metro') || gs.hasRole('admin') but is not working.

 

u_buiness_unit = is a custom field on the core company table

I am trying to only let admin and anyone that belongs to Metro see the UI action

 

 

1 ACCEPTED SOLUTION

Anks26
Kilo Sage

Hey Ricardo, 

As Tiagomacul suggested try using script include like this

UI condition:

 

new metroUsers().isMetro(gs.getUserID());

 


Script include:

 

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

    isMetro: function(user) {
        var gr = new GlideRecord('sys_user');
        gr.get(user);
        if (gr.manager.last_name.toString().indexOf('Bordwell') > -1) { // I am just checking last name of the manager as an example
            return true;
        }
    },

    type: 'metroUsers'
};

 

 

Also, you are missing parenthesis in your query '&&' is higher than '||'. 

 

(gs.hasRole("itil") && // added opening parenthesis 
  new GlideRecord("sys_user")
    .get(gs.getUserID())
    .company.u_business_unit.getValue()
    .includes("Metro")) || // added a closing parenthesis
  gs.hasRole("admin");

 


When debugging UI action condition, I always try to break it down one condition at a time to check which condition is breaking it, just a tip that may help in future.

Thanks
Anks

View solution in original post

3 REPLIES 3

DrewW
Mega Sage
Mega Sage

You need a space between new and GlideRecord, but I don't think the system is going to like an inline GlideRecord like that.  I'm sure you could use a GlideQuery inline like that.  I have done that before but I'm not finding my example at the moment.

https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server/no-namespace/GlideQueryAPI

 

Something like this is what I was thinking for the GlideQuery.

(new GlideQuery("core_company").where("sys_id", gs.getUser().getCompanyID()).where("u_business_unit", "CONTAINS", "Metro").select("sys_id").toArray(1)).length >= 1
 
because if you design the query right the array will have 1 row if its true and zero rows if false.
 

tiagomacul
Giga Sage

Use a script include, easier way also reusable.


https://www.servicenow.com/community/developer-blog/use-script-include-in-a-condition-field/ba-p/227...


https://docs.servicenow.com/bundle/tokyo-application-development/page/script/server-scripting/concep...

 

Function/Method Return Value Usage
gs.getUser() Returns a reference to the user object for the currently logged-in user. var userObject = gs.getUser();
gs.getUserByID() Returns a reference to the user object for the user ID (or sys_id) provided. var userObject = gs.getUser().getUserByID('employee');
gs.getUserName() Returns the User ID (user_name) for the currently logged-in user.
e.g. 'employee'
var user_name = gs.getUserName();
gs.getUserDisplayName() Returns the display value for the currently logged-in user.
e.g. 'Joe Employee'
var userDisplay = gs.getUserDisplayName();
gs.getUserID() Returns the sys_id string value for the currently logged-in user. var userID = gs.getUserID();
getFirstName() Returns the first name of the currently logged-in user. var firstName = gs.getUser().getFirstName();
getLastName() Returns the last name of the currently logged-in user. var lastName = gs.getUser().getLastName();
getEmail() Returns the email address of the currently logged-in user. var email = gs.getUser().getEmail();
getDepartmentID() Returns the department sys_id of the currently logged-in user. var deptID = gs.getUser().getDepartmentID();
getCompanyID() Returns the company sys_id of the currently logged-in user. var companyID = gs.getUser().getCompanyID();
getCompanyRecord() Returns the company GlideRecord of the currently logged-in user. var company = gs.getUser().getCompanyRecord();
getLanguage() Returns the language of the currently logged-in user. var language = gs.getUser().getLanguage();
getLocation() Returns the location of the currently logged-in user. var location = gs.getUser().getLocation();
getDomainID() Returns the domain sys_id of the currently logged-in user (only used for instances using domain separation). var domainID = gs.getUser().getDomainID();
getDomainDisplayValue() Returns the domain display value of the currently logged-in user (only used for instances using domain separation). var domainName = gs.getUser().getDomainDisplayValue();
getManagerID() Returns the manager sys_id of the currently logged-in user. var managerID = gs.getUser().getManagerID();
getMyGroups() Returns a list of all groups that the currently logged-in user is a member of. var groups = gs.getUser().getMyGroups();

 

https://servicenowguru.com/scripting/user-object-cheat-sheet/

 

Anks26
Kilo Sage

Hey Ricardo, 

As Tiagomacul suggested try using script include like this

UI condition:

 

new metroUsers().isMetro(gs.getUserID());

 


Script include:

 

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

    isMetro: function(user) {
        var gr = new GlideRecord('sys_user');
        gr.get(user);
        if (gr.manager.last_name.toString().indexOf('Bordwell') > -1) { // I am just checking last name of the manager as an example
            return true;
        }
    },

    type: 'metroUsers'
};

 

 

Also, you are missing parenthesis in your query '&&' is higher than '||'. 

 

(gs.hasRole("itil") && // added opening parenthesis 
  new GlideRecord("sys_user")
    .get(gs.getUserID())
    .company.u_business_unit.getValue()
    .includes("Metro")) || // added a closing parenthesis
  gs.hasRole("admin");

 


When debugging UI action condition, I always try to break it down one condition at a time to check which condition is breaking it, just a tip that may help in future.

Thanks
Anks