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

Popular posts from this blog

What is Business Rules?

What is Business Rules - ServiceNow? Business Rule is server side scripting that executes whenever a record is inserted, updated, deleted, displayed or queried. The key thing is to keep in the mind while creating a business rule is that when and on what action it has to execute. You can run the business rule ‘on display’, ‘on before’ or ‘on after’ of an action (insert, delete, update) is performed.   It executes on the server and does not monitor form fields. When to run Business Rules : 1) Display :  Runs when the form loads. Primary purpose is to populate the g_scratchpad global object. Use to provide client-side scripts access to server-side data Display Business Rules 2) Before : Executes after form submission and before the record update in the database. Use to update information on the current object. Example, a Business Rule containing current.state=3; would set the state field on the current record to the sta...

What is ServiceNow?

What is ServiceNow? ServiceNow  is a cloud based ITSM tool which provides service management software as a service and greater flexibility in terms of functionality, customization, reliability etc. When it was first introduced it was an ITSM tool but now it more than just ITSM tool.  ServiceNow  also caters for  IT Operation Management ,  IT Business Management . It has robust cloud platform on top of which, any application can be built quickly. ServiceNow Platform is one of the biggest strengths of ServiceNow. What is ServiceNow? You can build a different type of application on this platform for HR, Finance, Security, Customer Service etc. This tool has already captured a great market share. Best thing is while implementing this tool in your organization you don’t need to think about infrastructure, servers, database, data centers, security etc all these are taken care by ServiceNow itself. you just need to focus on im...