servicenow calculated field script

ServiceNow Calculated Field Script

By David Rodriguez
AD

ServiceNow calculated fields are a powerful feature that allows you to extend the functionality of your forms and tables by performing custom calculations based on other fields. This can streamline your processes, improve data accuracy, and enhance user experience. Let's delve into the world of ServiceNow calculated field scripts, their benefits, and how to create them.

In most cases, calculated fields automate repetitive tasks or generate results based on existing data. For instance, if you have an order form with fields for quantity and price, a calculated field can automatically calculate the total cost. This not only saves users the trouble of manual calculation but also reduces human error.

Understanding Calculated Field Scripts

Calculated field scripts are written in JavaScript and executed on the client-side. They use the current values of tables and fields to perform calculations, transformations, or validations. Understanding the basics of JavaScript is essential for writing effective calculated field scripts.

AD
ServiceNow Scripting Locations
ServiceNow Scripting Locations
How to Break Into ServiceNow in 2026
How to Break Into ServiceNow in 2026
Script Includes Explained
Script Includes Explained

ServiceNow provides a script editor with syntax highlighting and auto-complete suggestions to make scripting easier. Plus, you can test your scripts right within the editor to ensure they're working as expected.

Creating a Simple Calculated Field

Let's create a simple calculated field script that multiplies two numeric fields, Quantity and Price, to display the Total Cost. Here are the steps:

ServiceNow Scripting Guide
ServiceNow Scripting Guide
ServiceNow Scripting Myths
ServiceNow Scripting Myths
Workday Calculated Fields: The Logic Behind Smart Workflows
Workday Calculated Fields: The Logic Behind Smart Workflows

1. In the ServiceNow navigator, go to the table where you want to add the calculated field (e.g., Order).

2. Under the 'Form Designer' section, click 'Add Field'. Choose 'Calculated' from the list of field types.

AD

Scripting the Calculation

Once you've added the calculated field, you'll see a new script editor. To script the multiplication of Quantity and Price, enter the following code:

Personal ServiceNow Syntax Macros Cheat Sheet
Personal ServiceNow Syntax Macros Cheat Sheet
Is Your ServiceNow Application Developer Career Future-Proof?
Is Your ServiceNow Application Developer Career Future-Proof?
GlideRecord Best Practices
GlideRecord Best Practices
ServiceNow: Service Catalog
ServiceNow: Service Catalog
ServiceNow ITOM Cheat Sheet: The Complete Visual Guide to Discovery, CMDB, Service Mapping, Event Management, Cloud Discovery, Integrations, ...
ServiceNow ITOM Cheat Sheet: The Complete Visual Guide to Discovery, CMDB, Service Mapping, Event Management, Cloud Discovery, Integrations, ...
the screenshot shows that there are many options to use
the screenshot shows that there are many options to use

quantity * price

The 'quantity' and 'price' variables refer to the current values of the Quantity and Price fields. ServiceNow automatically converts these values into numbers before performing the calculation.

AD

Advanced Calculated Field Scripts

Calculated field scripts can perform a wide range of operations, not just simple arithmetic. They can parse dates, convert currencies, perform string manipulations, and even validate data based on complex rules. Let's explore a few advanced use cases.

an uml diagram showing the different tasks that each employee can use to complete their task
an uml diagram showing the different tasks that each employee can use to complete their task

Formatting Date and Time

ServiceNow allows you to format dates and times in calculated fields. For instance, you can display a date in a 'Month Day, Year' format like 'March 14, 2022' instead of '2022-03-14'. Here's an example script:

gs.dateFormat(new GlideDateTime(arguments).getTime(), 'MMMM dd, yyyy')

AD

Validating Inputs

Calculated fields can validate user inputs and display errors if the input is invalid. This can help ensure data integrity and provide helpful feedback to users. Here's an example script that checks if the Quantity field is a positive integer:

if (quantity > 0 && Number.isInteger(quantity)) { return 'Valid input'; } else { throw 'Quantity must be a positive integer'; }

Remember, calculated field scripts execute on the client-side, so they should be as lean and efficient as possible. Avoid performing complex, resource-intensive operations that could slow down your users' experience.

Calculated fields are a powerful tool in ServiceNow's arsenal, offering a-host of possibilities for automating tasks and enhancing data accuracy. Whether you're a seasoned scripter or just starting out, there's always more to explore in the world of calculated fields. So, go ahead and script away!

AD