Need g_user and g_form tips

Dinesh Kumar C
Tera Contributor

Hi folks ,

I am bit new to servicenow scripting , I am trying to write ui policies by using client scripts .

1)Could anyone explain where exactly we use client side functions like g_form and g_user. ?

2)what is exactly difference between g_user.hasRole , g_user.hasRoles() , , g_user.hasRoleExactly() , g_user.hasRoleFromList and in which scenarios we use these methods .

ThankYou In Advance .

1 ACCEPTED SOLUTION

haseena ayesha
Kilo Guru

1)Could anyone explain where exactly we use client side functions like g_form and g_user. ?


g_form :


GlideForm.js is the Javascript class used to customize forms.


g_form is a global object in the GlideForm class.


GlideForm.js and g_form are only used on the client (for instance, the web browser).


These methods are used to make custom changes to the form view of records. All validation of examples was done using Client Scripts.


Some of these methods can also be used in other client scripts (such as Catalog Client Scripts or Wizard Client Scripts), but must first be tested to determine whether they will work as expected.


g_user :



g_user is a global object in GlideUser (the Javascript class).  


g_user


  • can   only be used in Client Scripts.
  • contains name and role information about the current user.
  • is typically used in Client Scripts and UI Policies but is also found in UI Actions which run on the client.
  • cannot be used in Business Rules or UI Actions which run on the server.
  • avoids the need for much slower user queries (for example, GlideRecord queries to get user information)

Session information about the current user and current user roles is contained in the client (for example, web browser). All methods below [except getClientData()] access the session information that is available by default. getClientData() requires setup on the server and use of putClientData() to make session information available.



2)what is exactly difference between g_user.hasRole , g_user.hasRoles() , , g_user.hasRoleExactly() ,and in which scenarios we use these methods ?



g_user.hasRole() :


True if the current user has the role specified, false otherwise. ALWAYS returns true if the user has the 'admin' role.


ex: g_user.hasRole('itil')



g_user.hasRoles()


True if the current user has at least one role specified, false otherwise.


ex: g_user.hasRoles('itil','admin')



g_user.hasRoleExactly()


True if the current user has the exact role specified, false otherwise, regardless of 'admin' role.


ex:g_user.hasRoleExactly('itil');



formore information refer servicenow wiki


GlideForm (g form) - ServiceNow Wiki


GlideUser (g user) - ServiceNow Wiki


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



Thanks & Regards


Haseena


                                                                                                                                                      PS: Hit like, Helpful or Correct depending on the impact of the response


View solution in original post

3 REPLIES 3

haseena ayesha
Kilo Guru

1)Could anyone explain where exactly we use client side functions like g_form and g_user. ?


g_form :


GlideForm.js is the Javascript class used to customize forms.


g_form is a global object in the GlideForm class.


GlideForm.js and g_form are only used on the client (for instance, the web browser).


These methods are used to make custom changes to the form view of records. All validation of examples was done using Client Scripts.


Some of these methods can also be used in other client scripts (such as Catalog Client Scripts or Wizard Client Scripts), but must first be tested to determine whether they will work as expected.


g_user :



g_user is a global object in GlideUser (the Javascript class).  


g_user


  • can   only be used in Client Scripts.
  • contains name and role information about the current user.
  • is typically used in Client Scripts and UI Policies but is also found in UI Actions which run on the client.
  • cannot be used in Business Rules or UI Actions which run on the server.
  • avoids the need for much slower user queries (for example, GlideRecord queries to get user information)

Session information about the current user and current user roles is contained in the client (for example, web browser). All methods below [except getClientData()] access the session information that is available by default. getClientData() requires setup on the server and use of putClientData() to make session information available.



2)what is exactly difference between g_user.hasRole , g_user.hasRoles() , , g_user.hasRoleExactly() ,and in which scenarios we use these methods ?



g_user.hasRole() :


True if the current user has the role specified, false otherwise. ALWAYS returns true if the user has the 'admin' role.


ex: g_user.hasRole('itil')



g_user.hasRoles()


True if the current user has at least one role specified, false otherwise.


ex: g_user.hasRoles('itil','admin')



g_user.hasRoleExactly()


True if the current user has the exact role specified, false otherwise, regardless of 'admin' role.


ex:g_user.hasRoleExactly('itil');



formore information refer servicenow wiki


GlideForm (g form) - ServiceNow Wiki


GlideUser (g user) - ServiceNow Wiki


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



Thanks & Regards


Haseena


                                                                                                                                                      PS: Hit like, Helpful or Correct depending on the impact of the response


Runjay Patel
Giga Sage

Check out this video, it will clear all your doubts and help you to understand Client Script queries in details.

Link: https://www.youtube.com/watch?v=Wj8ZAWtTBqo&ab_channel=ServiceNowHelpdesk

It help you to understand below points.

  • UI policy and Client script
  • why client script run first in service now?.
  • Which rule execute first client Scripts or UI polices?
  • g_user and g_form tips
  • Difference between client script and script include (server).
  • Scenario based implementation
  • Understanding below
    • onLoad()
    • onChange()
    • onSubmit()
    • onCellEdit()

 

Please mark reply as Helpful/Correct, if applicable. Thanks!!