CRM Unit 3 Notes
CRM Unit 3 Notes
Here's how Client-side scripting & policies (UI and Data) are implemented in ServiceNow:
ServiceNow provides several types of client-side scripts to control the behavior and interactivity
of the UI:
Client Scripts: Client Scripts are scripts that run on the user’s browser, allowing you to
interact with fields, forms, and UI elements dynamically. These scripts can be triggered
by form events such as onLoad, onChange, onSubmit, and onCellEdit. They are
written in JavaScript and provide functionality like field validation, setting field values,
or showing/hiding fields based on certain conditions.
o onLoad: Runs when the form is loaded. Often used to set default values or apply
styling.
o onChange: Runs when a field value is changed. Commonly used for real-time
field validation or to dynamically update other fields.
o onSubmit: Runs when the form is submitted. Can be used for client-side
validation before submitting to the server.
o onCellEdit: Used in record lists (e.g., in a report or list view) to execute actions
when a cell value is changed.
UI Scripts: UI Scripts are custom JavaScript libraries that can be included in
ServiceNow to add reusable functionality that can be used across multiple UI Scripts or
Client Scripts. They are helpful when you need to write code that should be shared across
multiple scripts or when you want to centralize logic for better maintainability.
UI Actions: UI Actions are scripts triggered by user actions like clicking a button or
selecting a menu item. They often interact with the UI and data, providing users with
custom actions like opening new forms, triggering workflows, or calling other processes.
UI Actions can run both client-side and server-side code, and it's common to see a mix of
both in a single UI Action.
UI Templates: These allow developers to create reusable UI elements and components
for consistent use across forms or pages.
2. UI Policies in ServiceNow
UI Policies allow you to define conditions and actions for the user interface without the need for
scripting. They are typically used for things like:
Making Fields Mandatory or Read-Only: You can use UI Policies to control when
fields should be mandatory or read-only based on certain conditions (e.g., making a
"Priority" field mandatory when "Category" is set to "Incident").
Hiding/Showing Fields: You can conditionally hide or show fields to streamline the
form and ensure that only relevant information is presented to the user.
UI Policy Actions: These are actions that you define within a UI Policy to set the
behavior of fields (like mandatory, visible, or read-only) when certain conditions are met.
UI Policies work purely on the client-side, and while they don't require scripting, they can
significantly enhance the user experience by ensuring data is captured correctly or providing
dynamic field behaviors based on context.
Data Policies are similar to UI Policies, but they govern the data itself, typically focusing on
data integrity and ensuring that the data is valid before it is saved to the database. Unlike UI
Policies, Data Policies apply to both client-side and server-side operations.
Field Validation: You can set up Data Policies to ensure fields meet certain criteria (e.g.,
ensuring that a phone number is in the correct format before it’s saved).
Mandatory Fields: Data Policies can enforce whether a field is mandatory based on
specific conditions (similar to UI Policies but at the data level).
Conditionally Enforcing Rules: You can enforce rules on fields like ensuring that a
"Location" field must be populated when a "Category" is set to "Hardware," for example.
Unlike UI Policies, Data Policies are intended to handle business rules and ensure data integrity,
ensuring that the rules apply regardless of whether the data is coming from a client script or an
API.
Client Scripts and UI Policies: Client Scripts can interact with UI Policies. For example,
a client script might modify a field’s value or trigger some behavior that causes a UI
Policy to be applied (e.g., making a field read-only or mandatory based on dynamic
conditions).
Client Scripts and Data Policies: While Data Policies primarily run on the server side,
client scripts can trigger them by invoking the necessary conditions or ensuring that the
right data is sent to the server. If a client script sends invalid data or misses a required
field, the Data Policy can reject it before it's saved to the database.
By understanding how Client Scripts, UI Policies, and Data Policies interact and their roles in
managing both UI and data in ServiceNow, you can create a smooth, efficient, and secure
experience for users.
In ServiceNow, UI Actions are powerful tools that allow you to create buttons, context menu
items, and links that perform specific actions on records. UI Actions can be executed on both the
server-side (using Business Logic and server-side scripting) and the client-side (using UI
Script or client-side logic). Modularizing your programming using UI Actions allows for better
code reusability, organization, and maintainability.
Here’s how you can modularize your programming using UI Actions in ServiceNow, both on
the server-side and client-side:
1. Overview of UI Actions
UI Actions are typically used to create custom buttons or links on forms or lists.
They can execute actions based on user interaction (like clicking a button or selecting a menu
option).
UI Actions are usually associated with a table and can trigger scripts to perform operations such
as record updates, invoking workflows, sending notifications, or even opening other forms.
Server-side UI Actions allow you to encapsulate logic on the server side and trigger processes or
database operations.
Example of Server-Side UI Action:
Let's say you want a "Close Incident" button that should update the Incident record when
clicked, changing its state to "Closed".
2. Server-Side Script: In the Script field of the UI Action, write your server-side script to
perform the necessary operations when the button is clicked. This could include updating
the record, calling workflows, or performing business logic.
Modularization: To keep this script modular, consider moving complex or reusable logic
into Script Includes. For example, if you have common logic for updating incident states
across multiple actions, you can create a Script Include to encapsulate this functionality.
Script Include (Reusable Code): Let’s modularize the script by creating a Script Include
that handles the logic of closing incidents.
Client-side UI Actions allow you to create buttons or actions that perform operations on the
client side, such as opening forms, showing popups, or manipulating UI elements without
needing to reload the page.
Let’s say you want to add a button to the Incident form that shows an alert when clicked.
If the client-side script is complex or reusable, consider moving the logic into a UI
Script.
For example, you can create a UI Script (e.g., IncidentFormUtils) that contains
common functions for working with Incident forms, such as showing alerts, validating
fields, or populating dropdowns.
Server-Side Logic: Script Includes are executed on the server side and are not directly accessible
from the client (i.e., from the browser). They provide functionality to other scripts in the
platform.
Reusability: They help encapsulate and organize logic so that it can be reused across multiple
scripts.
Global or Scoped: Script Includes can either be global (available to all applications) or scoped
(available only within a specific application).
Asynchronous or Synchronous: Script Includes can be written to support either synchronous
(blocking) or asynchronous (non-blocking) behavior.
When you have common logic that needs to be reused across different scripts (e.g., business
rules, UI actions).
To keep your code modular and maintainable.
To interact with ServiceNow's Glide API and perform operations on data or system
configurations.
To call a Script Include in server-side scripts (like Business Rules or UI Actions), you
instantiate the class and then call its methods.
To call a Script Include from the client side, you use GlideAjax. This allows you to execute
server-side methods from client scripts.
Modularity: Keep your Script Includes small and focused on specific tasks. One class should
ideally handle one aspect of logic (e.g., Incident management, User management).
Naming Convention: Name your Script Includes clearly (e.g., IncidentUtils, UserHelper),
and consider naming methods to match their purpose.
Avoid Client-Side Logic: Script Includes should be used primarily for server-side logic. If client-
side logic is needed, use UI Scripts or Client Scripts.
Error Handling: Always include error handling and logging (using gs.error() or gs.info())
to help with debugging and monitoring.
Security Considerations: Be mindful of who can access your Script Includes, especially if you're
making them client-callable. Use proper validation to avoid unauthorized access.