- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
on 01-12-2023 01:05 AM
Hi All,
Hope you are doing great.
In todays article we will try to understand the Client script in ServiceNow with examples.
What is ServiceNow Client Script ?
- ServiceNow is a platform that offers a range of cloud-based services, including IT service management and IT operations management.
- A client script in ServiceNow is a type of script that runs on the client side, meaning it is executed in the web browser of the person accessing the ServiceNow platform.
- Client scripts are used to customize the behavior of a form or other UI element, and can be triggered by a user action such as clicking a button or by an event such as the loading of a page.
- Client scripts can be used to validate user input, set default values for fields, or perform other tasks to enhance the user experience on the ServiceNow platform.
What are different types of Client script in ServiceNow ?
There are several different types of client scripts in ServiceNow, including:
-
OnLoad scripts: These scripts are executed when a form or page is loaded in the web browser. OnLoad scripts can be used to set default values for fields, modify the behavior of UI elements, or perform other tasks to prepare the form or page for user interaction.
-
OnChange scripts: These scripts are executed when the value of a form field is changed by the user. OnChange scripts can be used to perform validation on user input, update other fields based on the new value, or take other actions in response to the change.
-
OnSubmit scripts: These scripts are executed when a user submits a form by clicking the Save button or performing another action that triggers a submission. OnSubmit scripts can be used to perform final validation on the form data, gather additional information, or perform other tasks before the form data is saved to the ServiceNow database.
-
OnCellEdit scripts: These scripts are executed when a user edits the value of a cell in a list or table. OnCellEdit scripts can be used to validate the new value, update other cells based on the new value, or take other actions in response to the edit.
Example of onLoad Client script
1. OnLoad Client Script :
-
An example of an onLoad client script in ServiceNow might look like this:
function onLoad() { // Set the default value for the "Priority" field g_form.setValue('priority', '3 - Low'); // Hide the "Assignment Group" field if the "Assigned to" field is empty if (g_form.getValue('assigned_to') == '') { g_form.setDisplay('assignment_group', false); } }
-
In this example, the onLoad script is executed when the form is loaded in the web browser. The script sets the default value for the "Priority" field to "3 - Low", and then checks the value of the "Assigned to" field. If the "Assigned to" field is empty, the script hides the "Assignment Group" field.
2. onChange Client Script :
- An example of an onChange client script in ServiceNow might look like this:
function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } // If the user changes the value of the "Category" field, // set the default value for the "Subcategory" field if (g_form.getValue('id') == 'category') { if (newValue == 'Hardware') { g_form.setValue('subcategory', 'Laptop'); } else if (newValue == 'Software') { g_form.setValue('subcategory', 'Operating System'); } } }
- In this example, the onChange script is executed whenever the user changes the value of a form field. The script checks the ID of the field that was changed, and if it is the "Category" field, it sets the default value for the "Subcategory" field based on the new value of the "Category" field. For example, if the user selects "Hardware" as the category, the script sets the default value for the subcategory to "Laptop".
3. onSubmit Client script :
- An example of an onSubmit client script in ServiceNow might look like this:
function onSubmit() { // Perform final validation on the form data if (g_form.getValue('category') == '') { g_form.showFieldMsg('category', 'Category is required', 'error'); return false; } // Gather additional information before the form is submitted g_form.setValue('caller_id', g_user.userID); g_form.setValue('opened_at', g_form.getCurrentDateTime()); // Return true to indicate that the form should be submitted return true; }
- In this example, the onSubmit script is executed when the user clicks the Save button to submit the form. The script first performs final validation on the form data, making sure that the "Category" field has a value. If the "Category" field is empty, the script displays an error message and returns false to prevent the form from being submitted. If the "Category" field has a value, the script gathers additional information (the caller ID and the time the form was submitted) and sets these values in the form. Finally, the script returns true to indicate that the form should be submitted to the ServiceNow database.
4. OnCellEdit Client Script :
- An example of an onCellEdit client script in ServiceNow might look like this:
function onCellEdit(sysIDs, table, oldValues, newValue, callback) { var saveAndClose = true; var isAdmin = g_user.hasRole('admin'); if ((!isAdmin && newValue == 2) || (!isAdmin && newValue == 3)){ alert('Not allowed to set this state'); saveAndClose = false; } callback(saveAndClose); }
- In this example we are restricting the updating of state field from lissst view if a person is not an admin.
Benefits of Client Script :
There are several benefits to using client scripts in ServiceNow, including:
-
Customization: Client scripts allow you to customize the behavior of forms and other UI elements in the ServiceNow platform, making it easier to tailor the platform to your specific needs and requirements.
-
User experience: Client scripts can be used to enhance the user experience on the ServiceNow platform, by providing additional functionality and making it easier for users to interact with forms and other UI elements.
-
Efficiency: Client scripts can help to automate and streamline common tasks and processes, making it faster and easier for users to complete tasks and access the information they need.
-
Flexibility: Client scripts provide a versatile and powerful way to customize the ServiceNow platform, and can be used in a wide range of scenarios and contexts to meet the specific needs of your organization.
Best Practices :
Here are some best practices for using client scripts in ServiceNow:
-
Use the correct script type: Choose the appropriate script type for your needs, based on the trigger and the type of customization you want to perform. For example, use an onLoad script for tasks that should be performed when a form or page is loaded, and an onChange script for tasks that should be performed in response to a user changing a form field value.
-
Keep scripts short and focused: Try to keep your client scripts short and focused on a specific task or set of tasks. This can make your scripts easier to read, understand, and maintain, and can help to avoid performance issues and other problems.
-
Use the right script context: Make sure to use the correct script context (global, on the form, or on the table) for your client scripts. This can help to ensure that your scripts have access to the right variables and functions, and can prevent errors and other problems.
-
Test your scripts thoroughly: Test your client scripts thoroughly to make sure they work as expected and don't cause any problems. This can help to ensure that your scripts provide the intended functionality and don't cause any issues for users or the ServiceNow platform.
-
Use the g_form and g_user objects wisely: Use the g_form and g_user objects wisely in your client scripts. These objects provide access to a wide range of information and functionality, but can also cause performance issues if used excessively or in inefficient ways.
Please be sure to bookmark this article as well as mark it as Helpful if you thought it was helpful.
Regards,
Amit Gujarathi
- 18,366 Views
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
I really hate doing this because of the time and effort you put into writing the post, BUT, I just had to point out that the script examples are not very good, especially for new users just learning the platform. Here’s why:
1. onLoad Client Script:
- A default value should normally be set once, when creating the record. This script will blindly set the Priority value each time the record is viewed.
- Default values should be set on the Dictionary Entry or Dictionary Override records. Another way to do it is documented here: TNT: Setting Default Values Without Messing Up OOB Fields.
- Any ticket/case should always be assigned to a Group and not necessarily a particular person throughout its lifecycle, so you have the logic backwards on the hiding of the Assignment Group field. And that really should be a UI Policy instead of a script so that it continuously fires on changes to the field instead of just once onLoad.
2. onChange Client Script:
- Why are you checking the “id” of the field being changed? onChange scripts are configured to fire when a specific field is changed. And I don’t even know what that “id” field would be.
- Because JavaScript is case-sensitive, the example wouldn’t show the expected results for users with an OOB setup. The “value” for “Hardware” is actually “hardware” with a lowercase “h” and “Operating System” is “os”. Nitpicky, yes, I know, but for new users the results would be confusing and you’ll just end up with a bunch of questions.
3. onSubmit Client Script:
- checking if the Category field contains data is better served by a UI Policy or defined on the Dictionary Entry or Dictionary Override records
- setting the Caller field to the current user each time the record is saved/updated makes no sense
4. onCellEdit Client Script:
- the description is not actually 100% accurate. The script stops the update only if the user is not an admin AND the value is being changed to “In Progress” or “On Hold”. It would allow other changes, which probably does not make sense.
- a requirement like that should be solved by an Access Control instead. The problem with a Client Script is the user can change the value BUT then is warned afterwards and the change is reversed. Not a very good user experience. One benefit of an Access Control would be not even letting the user attempt to change it.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
hello i checked on submit example but i am getting warning regarding
onSubmit script error: TypeError: g_form.getCurrentDateTime is not a function:
function () { [native code] }
pls help wat to do next where to correct
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content