How to set a value on a field of the form or record
In this Article, you will learn how can you set a value on a field of a form on different conditions and scenarios.
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.
Script Type : Client Script
Name : Populate Caller on the Incident
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);
}
}
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 of Category field changes to Hardware.
Script Type : Client Script
Name : Populate Caller on the Incident
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');
}
}
Labels:
https://www.servicenow.com/community/developer-articles/how-to-set-a-value-on-a-field-of-the-form-or-record/ta-p/2321005
