These are just my personal notes as I was working through the New to ServiceNow training modules.
In All > Studio I selected my app (NeedIt) and then selected Client Development > Client Scripts > NeedIt onLoad Example.
And then set the NeedIt onLoad Example to be Active. The script in this example will create a pop-up alert onLoad (when the NeedIt app is opened).
I then opened the NeedIt app and got the pop-up alert.
Likewise, the NeedIt onChange example creates a pop-up alert when a change is made to the "Short description" field.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
alert("You have changed the Short description from " + oldValue + " to " + newValue + ".");
}
And the NeedIt onSubmit example created a pop-up alert when the Submit button is clicked.
function onSubmit() {
alert("Thank you for submitting a NeedIt request.");
}
To create your own client side script, in Studio, select Create Application File.
Select Client Development > Client Script > Create.
And then used the following.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if ( newValue == '') {
return;
}
var whatneeded = g_form.getValue('u_what_needed');
// Clear all of the choices from the What needed field choice list
g_form.clearOptions('u_what_needed');
// If the value of the Request type field is hr, add
// two hr choices and other to the What needed field choice list
if(newValue == 'hr'){
g_form.addOption('u_what_needed','hr1','Human Resources 1');
g_form.addOption('u_what_needed','hr2','Human Resources 2');
g_form.addOption('u_what_needed','other','Other');
}
// If the value of the Request type field is facilities, add
// two facilities choices and other to the What needed field
// choice list
if(newValue == 'facilities'){
g_form.addOption('u_what_needed','facilities1','Facilities 1');
g_form.addOption('u_what_needed','facilities2','Facilities 2');
g_form.addOption('u_what_needed','other','Other');
}
// If the value of the Request type field is legal, add
// two legal choices and other to the What needed field
// choice list
if(newValue == 'legal'){
g_form.addOption('u_what_needed','legal1','Legal 1');
g_form.addOption('u_what_needed','legal2','Legal 2');
g_form.addOption('u_what_needed','other','Other');
}
// If the form is loading and it is not a new record, set the u_what_needed value to the
// value from the record before it was loaded
if(isLoading && !g_form.isNewRecord()){
g_form.setValue('u_what_needed', whatneeded);
}
}
With this new script active, when I selected a Request type such as Human Resources, "What needed" automagically updated to an appropriate default selection.
Did you find this article helpful?
If so, consider buying me a coffee over at