The CreatorCon Call for Content is officially open! Get started here.

Amit Gujarathi
Giga Sage
Giga Sage

Hi There,

I hope you're doing great.

With the participation in the ServiceNow Javascripting Challenge led by @Gagan Jolly, I m learning a lot. I thought I should share my learning on the Now community also

Day 10 : ServiceNow Java scripting Challange

Statement of Work :

Write a code using the following methods of Glide User API

  1. firstName
  2. lastName
  3. userID
  4. userName
  5. getFullName
  6. hasRole
  7. hasRoleExactly
  8. hasRoleFromList
  9. hasRoles

Solution :

Let's first try to understand what ServiceNow Glide User API means

Glide User API :

  • provides access to information about the current user and current user roles
  • GlideUser API avoids the need to use the slower GlideRecord queries to get user information.
  • accesed throgh global object g_user

Lets look into different Glide User Methods :

  1. firstName
    • d
    • Method signature :
    • Input parameters :
    • Return
  2. firstName
    • Returns the current user's first name.
    • Method signature : g_user.firstName
    • Input parameters : NA
    • Return : NA
  3. lastName
    • The current user's last name.
    • Method signature : g_user.lastName
    • Input parameters : NA
    • Return : NA
  4. userID
    • Returns the sys_id of the current user.
    • Method signature : g_user.userID
    • Input parameters : NA
    • Return : NA
  5. userName
    • This property is the current user's username
    • Method signature : g_user.userName
    • Input parameters : NA
    • Return : NA
  6. getFullName
    • Returns the first and last name of the current user.
    • Method signature : getFullName()
    • Input parameters : void
    • Return : String =⇒ The current user's full name.
  7. hasRole
    • Returns true if the current user has the specified role or the admin role.
    • Method signature : hasRole(String role, Boolean includeDefaults)
    • Input parameters :
      • role =⇒ String =⇒
      • includeDefaults =⇒ Boolean =⇒ Consider default roles if its true
    • Return :
      • Boolean =⇒ Returns true if the current user has the specified role or the admin role; otherwise returns false.
  8. hasRoleExactly
    • Determines whether the current user has the specified role.
    • Method signature : hasRoleExactly(String role, Boolean includeDefaults)
    • Input parameters :
      • role =⇒ String =⇒
      • includeDefaults =⇒ Boolean =⇒ Consider default roles if its true
    • Return :
      • Boolean =⇒ Returns true if the current user has the specified role
  9. hasRoleFromList
    • Returns true if the current user has at least one of the specified roles or has the admin role.
    • Method signature : hasRoleFromList(String roles, Boolean includeDefaults)
    • Input parameters :
      • role =⇒ String =⇒
      • includeDefaults =⇒ Boolean =⇒ Consider default roles if its true
    • Return :
      • Boolean =⇒ Returns true if the current user has the specified role
  10. hasRoles
    • Returns true if the current user has any role.
    • Method signature : hasRoles(Boolean includeDefaults)
    • Input parameters :
      • includeDefaults =⇒ Boolean =⇒ Consider default roles if its true
    • Return :
      • Boolean =⇒ Returns true if the current user has at least one role.

 

function onLoad() {
	// Get Logged in user first name
	alert ('First name : '+g_user.firstName);
	// Get Logged in user last name
	alert ('Last name : '+g_user.lastName);
	// Get Logged in user user id
	alert ('USer Id : '+g_user.userID);
	// Get Logged in user user name
	alert ('User name : '+g_user.userName);
	// Get Logged in user full name
	alert ('Full name : '+g_user.getFullName());
	// Check user has roles
	alert ('hasRole :: '+g_user.hasRole('snc_internal',true));
	// Check user has roles exxactly
	alert ('has role exactly  :: '+g_user.hasRoleExactly('snc_internal',true));
	// Check user has roles out of the gggiven roles
	alert ('Has role from list :: '+g_user.hasRoleFromList('snc_internal,itil,maint,admin,abc',true));
	// Check user has roles
	alert ('HAs role : '+g_user.hasRoles(true));
}

 

Please be sure to bookmark this article and mark it as Helpful if you thought it helpful.

Regards,

Amit Gujarathi

Technomonk Youtube

Amit Gujarathi Linkedin

TheTechnomonk.com

ServiceNow Community Amit Gujarathi

Version history
Last update:
‎11-22-2023 09:14 PM
Updated by:
Contributors