Skip to main content

What is Client Scripts?

What is Client Scripts - ServiceNow?


Client scripts sits on the client side (the browser) and run there only instead of on the server. Client Scripts manage forms and fields in real time.

Examples include:

  •         Modify choice list options
  •         Hide/Show form sections
  •         Set one field in response to another in real time
  •         Make fields mandatory
  •         Hide fields

Client Scripts are of 4 types:



ServiceNow Client Scripts, Client scripts, Types of Client Scripts
ServiceNow Client Scripts


OnLoad():

  •      Script runs when a form meeting the trigger condition loads and before control is given to user.
  •       Typically used to manipulate a forms appearance or content.
  •       The onLoad() function template is automatically inserted in the Script field.
  •       The OnLoad function has no argument passed to it.

OnSubmit():

  •       Script runs when a form meeting the trigger condition is saved, updated, or submitted.
  •       Typically used for field validation.
  •       The onSubmit() function template is automatically inserted in the Script field
  •       The onSubmit() function has no arguments passed to it.

OnChange():

  •      Script runs when a particular field value on a form changes
  •      Typically used to respond to field values of interest
  •      The onChange() function template is automatically inserted in the Script field

OnCellEdit():

  •      Script runs when the list editor changes a cell value.
  •      Can be applied to multiple records 
  •      The onCellEdit() function template is automatically inserted in the script field


What data can I use in Client Scripts?


  •      Local variables declared in script
  •      Client Script Global Variables

  •      g_form
  •      g_user
  •      g_scratchpad

Common g_form Object Methods:


Method
Description
getValue('field_name')
Returns the value of the specified field.
setDisplay('field_name' , bool )
Sets the value of a given field name to a specified value.
setValue('field_name' , 'value')
Shows/hides a field on the form based on the Boolean argument.
setReadOnly('field_name', bool)
Makes a field read only.
setMandatory('field_name', bool)
Makes a field Mandatory.
addOption('field_name', 'value',
'Label', 'placement')
Lets you add an option to a choice list, optionally at a specific place in the list.
removeOption('field_name', 'Value_to_remove')
Removes the specified value option from a choice list
showFieldMsg ('field_name' ,'Message', 'info' , 'scrollBool')
Shows a message below the given field , optionally scroll the page to that location(true/false).Can be „info or „error type
hideFieldMsg('field_name')
Hides messages, if any for a given field
clearValue('field_name')
Clears the value of a field. This is essentially the same as setValue („field_name)
getUniqueValue()
Returns the sys_id of the displayed record returns true or false
isNewRecord()
Returns true or false
getTableName()
Returns the form type, i.e incident, change_request



  •      g_form.getValue() – Text Field
  •      g_form.getValue(‘number’) - Returns the contents of the Number(number) text field


  •      g_form.getValue() – Choice List
  •      g_form.getValue(‘priority’) - Returns the contents of the Priority (priority) Choice List selection

Common g_user Object Methods:


Method
Description
getFullName()
Returns the first and last name of the current user.
hasRole('role')
Returns true if the current user has the specific role or admin role. This method can be passed only one role.
hasRoleExactly('role')
Returns true if the current user has this specific role.
hasRoleFromList('role1' , 'role2')
Returns true the current user has at least one of the specified roles in the list or admin role.
hasRoles()
Returns true if the current user has any role.


Client Scripts Best Practices:


  •      Enclose Code in Functions.
  •      Avoid DOM manipulation.
  •      use g_form object.
  •      Avoid global client scripting.


Debugging Client Script:


·      ServiceNow Tools

  •     JavaScript Debugger
  •     Info/Error messages
  •     Response Time Indicator

·      JavaScript Tools

  •   Alerts
  •   Try/Catch

·      Browser Tools

  •    Firefox: Webconsole
  •    Chrome: Javascript Console
  •    IE: Developer tools





Comments

Post a Comment