Amit Gujarathi
Giga Sage
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 11-22-2023 09:14 PM
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
- firstName
- lastName
- userID
- userName
- getFullName
- hasRole
- hasRoleExactly
- hasRoleFromList
- 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 :
- firstName
- d
- Method signature :
- Input parameters :
- Return
- firstName
- Returns the current user's first name.
- Method signature : g_user.firstName
- Input parameters : NA
- Return : NA
- lastName
- The current user's last name.
- Method signature : g_user.lastName
- Input parameters : NA
- Return : NA
- userID
- Returns the sys_id of the current user.
- Method signature : g_user.userID
- Input parameters : NA
- Return : NA
- userName
- This property is the current user's username
- Method signature : g_user.userName
- Input parameters : NA
- Return : NA
- 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.
- 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.
- 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
- 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
- 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
Labels:
- 779 Views