How To Set A Default Field Value in ServiceNow?

The Dictionary Entry Records & Default Values In ServiceNow, you can default field values to improve automation efforts in your environment. Every field on every table, is a Dictionary Entry in ServiceNow. When you’re looking …

set a default field value at the dictionary level

Buy The "ServiceNow Developer's Manual" Now

We've packed over a decade of ServiceNow Development advice, into a single e-book.

Buy It Now

The Dictionary Entry Records & Default Values

In ServiceNow, you can default field values to improve automation efforts in your environment.

Every field on every table, is a Dictionary Entry in ServiceNow.

When you’re looking at a form, that form is contained of fields. Those fields are all configurable as “columns” on a table. Every one of those fields, is easily referenced back to the Dictionary Entry [sys_dictionary] table.

There are several important fields on the Dictionary entry record.

These fields include whether or not the field is active, whether it’s mandatory, read-only, what table it lives on, certain attributes, etc.

One of these very important fields is the “Default Value” field. The “Default Value” field is under the “Default Value” tab.

In the Default Value field, you can use JavaScript to set the value of that field.

Returning A Dynamic Default Value

For example, it probably makes sense to set the “Caller” field as the user that is submitting an incident.

You can even go further than this. You can write queries in a business rule for example, that returns a value to automatically set in that field.

That’s exactly what the out of box “caller_id” field on the Incident table does. It calls a business rule function and returns the users value, to automatically populate.

incident caller_id caller field dictionary

You can either use a “Dynamic default value” which calls a function in a business rule, or you can just use some JavaScript to set the value directly.

It really depends on whether or not you want to return a value immediately, or if you need to interact with a table in ServiceNow – and return a value from a query.

Here is what the referenced record looks like, that connects the dictionary entry to the business rule.

The “Script” and the “Reference Script” values need to be populated in order for this to work properly.

You need to populate the “Script” with the full function name of the function that’s returning a value in the business rule. Make sure the check the “Active” checkbox too.

call a function in a business rule from the dictionary

Below is what ServiceNow uses to get the current user, to populate the Caller on every incident.

get caller_id value of current user

So here is a benefit of using a query, to return a value. It’s a more custom way to get a value, if a single line of JavaScript won’t work.

Return A Value Using JavaScript or GlideSystem

This is the more common use case, in my opinion.

It’s more frequent that you’re going to write a piece of JavaScript as a default value, than to write a business rule to return a value from query.

return value with javascript at dictionary entry

If you’re familiar with the ServiceNow Glide System, you can utilize that to auto-populate a value with it.

For example, take any Date/Time field in ServiceNow that tracks the “Opened at” value.

You don’t want a user to modify this value, nor do you want the user to ever populate this value.

It should be auto populated one single time and then never updated again.

To accomplish this, you don’t need to write a client script or business rule. You can just set a Default Value that will work every time. Each time it’s referenced, when a record is created, the system calculates the default value at the dictionary level.

The above value will return the current date and time, and set that as the value of the field – automatically.

When you’re using JavaScript to set a field value, you just need to prepend the Javascript, with “javascript:” {enter JavaScript here}

For example, you’d use the following: setting a user id value as opened by user

Take the above example of wanting to set a value for the “opened_by” user on table.

We will always want to set the “opened_by” field to be the value of the user that is submitting the record, on insert.

So to capture that value and set it, we use the following:

javascript: gs.getUserID();

This auto populates the value in the field, without any need for manual intervention.

There are endless opportunities when using JavaScript and the GlideSystem to populate field values.

Let us know how you’re using Default Values at the dictionary level below.



What Do You Think About This Article?

0 0 votes
Article Rating
Subscribe
Notify of
guest
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
KC
KC
1 year ago

Hi! How about page load time in millisecond, how do we do it in default value? Say default value every time the page is loaded should be like 1680167597087. Thanks in advance.

The Rock
The Rock
6 months ago

How do I add something like “Record has been updated by ” javascript: gs.getUserDisplayName();

4
0
Would love your thoughts, please comment.x
()
x