GlideUser - Scoped

  • Release version: Xanadu
  • Updated August 1, 2024
  • 3 minutes to read
  • The GlideUser API provides methods to access information about the current user and current user roles.

    Using the GlideUser API avoids the need to use the slower GlideRecord queries to get user information.

    Scoped GlideUser - getCompanyID()

    Returns the current user's company sys_id.

    Table 1. Parameters
    Name Type Description
    None
    Table 2. Returns
    Type Description
    String Company sys_id.
    var currentUser = gs.getUser(); 
    gs.info(currentUser.getCompanyID());

    Scoped GlideUser - getDisplayName()

    Returns the current user's display name.

    Table 3. Parameters
    Name Type Description
    None
    Table 4. Returns
    Type Description
    String User's display name
    var currentUser = gs.getUser(); 
    gs.info(currentUser.getDisplayName());

    Scoped GlideUser - getEmail()

    Returns the user's email address.

    Table 5. Parameters
    Name Type Description
    None
    Table 6. Returns
    Type Description
    String User's email address.
    var currentUser = gs.getUser(); 
    gs.info(currentUser.getEmail());

    Scoped GlideUser - getFirstName()

    Returns the user's first name.

    Table 7. Parameters
    Name Type Description
    None
    Table 8. Returns
    Type Description
    String User's first name.
    var currentUser = gs.getUser(); 
    gs.info(currentUser.getFirstName());

    Scoped GlideUser - getID()

    Gets the sys_id of the current user.

    Table 9. Parameters
    Name Type Description
    None
    Table 10. Returns
    Type Description
    String User's sys_id.
    var currentUser = gs.getUser(); 
    gs.info(currentUser.getID());

    Scoped GlideUser - getLastName()

    Returns the user's last name.

    Table 11. Parameters
    Name Type Description
    None
    Table 12. Returns
    Type Description
    String User's last name.
    var currentUser = gs.getUser(); 
    gs.info(currentUser.getLastName());

    Scoped GlideUser - getName()

    Returns the user ID, or login name, of the current user.

    Table 13. Parameters
    Name Type Description
    None
    Table 14. Returns
    Type Description
    String User ID or login name.
    var currentUser = gs.getUser(); 
    gs.info(currentUser.getName());

    Scoped GlideUser - getPreference(String name)

    Gets the specified user preference value for the current user.

    Table 15. Parameters
    Name Type Description
    name String Name of the preference.
    Table 16. Returns
    Type Description
    String Preference value.
    var currentUser = gs.getUser(); 
    currentUser.savePreference(­'myPref','red'); 
    gs.info(currentUser.getPreference(­'myPref'));

    Scoped GlideUser - getRoles()

    Returns a list of roles that includes explicitly granted roles, inherited roles, and roles acquired by group membership.

    Table 17. Parameters
    Name Type Description
    None
    Table 18. Returns
    Type Description
    Array List of all roles available to the user
    var currentUser = gs.getUser(); 
    gs.info(currentUser.getRoles());

    Scoped GlideUser - getUserRoles()

    Returns the list of roles explicitly granted to the user.

    Unlike the getRoles() method, this method does not return roles the user inherits or roles acquired from group membership.

    Table 19. Parameters
    Name Type Description
    None
    Table 20. Returns
    Type Description
    Array List of roles explicitly assigned to the user.
    var currentUser = gs.getUser(); 
    gs.info(currentUser.getUserRoles());

    Scoped GlideUser - hasAssignedRole(String roleName)

    Returns true if the user has the specified role.

    Table 21. Parameters
    Name Type Description
    roleName String Checks if the user has this role.
    Note:
    For the current user, this method checks for the role in their elevated privilege roles only if they elevated to that role in the current session. For non current users, this method checks for the role in all of their elevated privilege roles.
    Table 22. Returns
    Type Description
    Boolean

    Flag that indicates whether the user has the specified role.

    Valid values:
    • true: The user has the specified role.
    • false: The user doesn't have the specified role.

    This example checks if the current user has the admin role.

    var currentUser = gs.getUser();
    gs.info(currentUser.hasAssignedRole('admin'));

    Output:

    true

    Scoped GlideUser - hasRole(String role)

    Returns true if the user has the specified role or the admin role.

    Table 23. Parameters
    Name Type Description
    role String Checks if the user has this role.
    Table 24. Returns
    Type Description
    Boolean

    Flag that indicates whether the user has the specified role or the admin role.

    Valid values:
    • true: The user has the specified role or the admin role.
    • false: The user doesn't have the specified role or the admin role.

    This example checks if the current user has the admin role.

    var currentUser = gs.getUser(); 
    gs.info(currentUser.hasRole('admin'));

    Output:

    true

    Scoped GlideUser - isMemberOf(String group)

    Determines if the current user is a member of the specified group.

    Table 25. Parameters
    Name Type Description
    group String Group to check.
    Table 26. Returns
    Type Description
    Boolean Flag that indicates whether the user is a member of the specified group.
    Possible values:
    • true: User is a member of the specified group.
    • false: User isn't a member of the specified group.
    var currentUser = gs.getUser(); 
    gs.info(currentUser.isMemberOf(­'Capacity Mgmt'));

    Scoped GlideUser - savePreference(String name, String value)

    Saves a user preference value to the database.

    Table 27. Parameters
    Name Type Description
    name String Preference to save.
    value String Preference value.
    Table 28. Returns
    Type Description
    None
    var currentUser = gs.getUser(); 
    currentUser.savePreference('myPref','red'); 
    gs.info(currentUser.getPreference('myPref'));