How to set a value on a field of the form or record in ServiceNow?

In this tutorial, you will learn how can you set a value on a field of a form on different conditions and scenarios.


Setting the field value on load of the form


Scenario: When incident form is loaded then the value of Caller field should populated with logged in user.

Solution : This type of requirement can be achieved with Client Script as it needs to be set when form is loaded in the browser.

Type of Script : Client Script

Name : Populate Caller on the Incident

Table : Incident

UI Type : All

Type : OnLoad

Active : True

Global: True

Script :


function onLoad() {
 //Type appropriate comment here, and begin script below


 if (g_form.isNewRecord()) {


 var currentUser = g_user.userID;


 g_form.setValue('caller_id', currentUser);
 }
}


Setting the field value on change of a field


Scenario: When incident form is loaded then the Assignment Group should be populated with Network group.

Solution : This type of requirement can be achieved with Client Script as it needs to be set when value oof Category field changes to Hardware.

Type of Script : Client Script

Name : Populate Caller on the Incident

Table : Incident

UI Type : All

Type : OnChange

Field: Category

Active : True

Global: True

Script :


function onChange(control, oldValue, newValue, isLoading, isTemplate) {
 if (isLoading || newValue === '') {
 return;
 }


 //Type appropriate comment here, and begin script below
 if (newValue == 'hardware') {
 //Setting SYSID OF Network Group
 g_form.setValue('assignment_group', '287ebd7da9fe198100f92cc8d1d2154e'
 ,'Network'); 
 }


}



176 views0 comments

Recent Posts

See All

How to send email reminders in ServiceNow

Use Case If active incident is not updated in last 7 days then user in assigned to field should get reminder to update the ticket with latest status. Solution ServiceNow can send reminder emails by us

How to create custom popup alert box?

This will show you how can you create custom popup with designer buttons by using Glide Modal in ServiceNow. Use Case An alert popup always shows default browser alert which is added by adding alert()