What is Client-Side and Server-Side Scripting in ServiceNow

karra
Tera Contributor

Client-side scripting in ServiceNow is triggered by actions within the user's browser, responding to events such as form loads, submissions, or field value changes. It utilizes JavaScript to enhance the user interface and interaction.

Key components include:

Client Script: Custom JavaScript that runs on the client side to apply logic based on user actions or form events.

UI Policies: Rules that dynamically change form information and behavior without writing a script.

UI Scripts: Reusable JavaScript code that can be included in pages and forms across the ServiceNow platform.

 

Server-side scripting in ServiceNow occurs when a user action leads to a request sent to the server, such as updating, inserting, or deleting records. Scripts running on the server can modify ServiceNow's functionality or appearance, enforce business logic, or handle record operations. Server-side scripting is powered by Glide APIs, offering a suite of classes and methods for executing tasks on the server.

 


Client Side APIs:

1. Glide Form (g_form): Used for manipulating and managing the form interface. It can get or set field values, add error messages, or change field properties.
 Ex: `g_form.setValue('short_description', 'Issue with login');`

2. Glide User: Allows access to information about the current user from the client side, such as user ID or display name.
Ex: `var userName = g_user.displayName;`

3. Glide Ajax: Enables the execution of server-side code from the client side asynchronously. It's used to retrieve data without reloading the page.
Ex:  var ga = new GlideAjax('HelloWorld');
  ga.addParam('sysparm_name', 'helloWorld');
  ga.getXML(doSomethingWithResponse);

4. Glide Dialog Window: Provides a way to create and control dialog windows. It's useful for displaying forms or information without navigating away from the current page.
Ex: `var dialog = new GlideDialogWindow('dialog_id'); dialog.render();`

5. Glide List: Allows manipulation and interaction with list fields on a form, including adding or removing items.
 Ex: `g_form.addOption('watch_list', 'sys_id', 'name');`

6. Glide List Menu: Used to interact with and customize the context menus in list views.
Ex: Adding custom menu items to a list's context menu.

Server Side APIs:

1. Glide Record: Enables database operations such as creating, reading, updating, and deleting records.
Ex: var gr = new GlideRecord('incident');
  gr.get('sys_id', 'someSysId');
  
2. Glide System: Offers methods for logging, querying system information, and other utility functions.
Ex: `gs.info('This is an info message');`

3. Glide Date: Provides methods for working with dates on the server side, excluding time.
Ex: var gd = new GlideDate();
  gd.setValue('2023-01-01');
  
4. Glide Date Time: Allows for manipulation of date and time, including calculations and formatting.
Ex: var gdt = new GlideDateTime();
  gdt.addDaysLocalTime(5);
  
5. Glide Aggregate: Used for aggregate queries, such as COUNT, SUM, AVG, MIN, and MAX on records.
Ex: var agg = new GlideAggregate('incident');
  agg.addAggregate('COUNT');
  agg.query();
 
6. Glide Element: This represents a field on a GlideRecord and allows for field-specific operations like changing the value or retrieving the display value.
Ex: `var incidentState = current.state.getDisplayValue();`

 

1 REPLY 1

abhijeet_dudhal
Tera Contributor

Client-Side Scripting Tips:

Glide Form (g_form): Essential for form interactions. E.g., set a field value based on another field's input.
Glide User: Use for user-specific logic or greetings.
GlideAjax: Key for server-side data without page refreshes. Ideal for form validations.
Glide Dialog Window: Great for creating pop-up dialogs for additional information without leaving the page.


Server-Side Scripting Essentials:

Glide Record: The foundation for database interactions. Use for creating, reading, updating, and deleting records.
Glide System (gs): Useful for logging and system information.
GlideAggregate: Optimizes data aggregation queries. Use for summaries or statistical data.
GlideElement: Deals with field-specific operations, like retrieving a field's display value.


General Best Practices:

Test extensively in a non-production environment.
Prioritize performance and user experience.
Comment your code for clarity and maintenance.
Keep scripts efficient and purposeful for the best balance between functionality and performance.