- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 07:15 AM
Hi there experts,
I am working on the following functionallity: I have created new field called "Critical Incidents" which is reference to Incident table. When the user selects a Business Service in the incident, we want the field Critical Incidents to be populated with all incidents which have the same Business Service and have Priority 1(Critical).
I have built that with client script and script include and my code is working except that when I clear the value from Business Service, the Critical Incidents still keeps the old value. Tried to add "if" condition in the client script but it is not working. Can you please guide me how to achieve that.
On change client script on "Business Service" field.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(!g_form.getValue('business_service')
{g_form.setValue("u_critical_incidents", '');}
var userName = new GlideAjax('TaskThree');
userName.addParam('sysparm_name', 'criticalIncidents');
userName.addParam('sysparm_var1', g_form.getValue('business_service'));
userName.getXMLAnswer(callback);
function callback(response) {
var answer = response;
g_form.setValue("u_critical_incidents", answer);
}
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 07:40 PM
Hi @EH Desev Modified your script, add the bold lines to keep null value
if (isLoading || newValue === '') { //clears field when there is no selection of Business Service
g_form.setValue("u_critical_incidents", '');
return;
}
var userName = new GlideAjax('TaskThree');
userName.addParam('sysparm_name', 'criticalIncidents');
userName.addParam('sysparm_var1', g_form.getValue('business_service'));
userName.getXMLAnswer(callback);
function callback(response) {
var answer = response;
g_form.setValue("u_critical_incidents", answer);
}
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 07:06 PM
Hello @EH Desev ,
I've observed the following in the provided code:
if (isLoading || newValue === '') {
alert("newValue : "+ newValue );
return;
}
If your "on change" client script is configured to trigger on the change of the 'Business Service' variable, and the value is empty, there is a possibility that your code execution is entering this check (if (newValue == '')). You may consider adding an alert statement at this point to inspect the flow. As a workaround, you could try removing this check and then execute the script. Please let me know if it works?
If my answer solves your issue, please mark it as Helpful 👍 and Accepted ✔️ based on impact.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 07:40 PM
Hi @EH Desev Modified your script, add the bold lines to keep null value
if (isLoading || newValue === '') { //clears field when there is no selection of Business Service
g_form.setValue("u_critical_incidents", '');
return;
}
var userName = new GlideAjax('TaskThree');
userName.addParam('sysparm_name', 'criticalIncidents');
userName.addParam('sysparm_var1', g_form.getValue('business_service'));
userName.getXMLAnswer(callback);
function callback(response) {
var answer = response;
g_form.setValue("u_critical_incidents", answer);
}
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 01:29 AM
Thanks, Harish! This resolved my problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-21-2023 07:48 PM - edited 12-21-2023 07:48 PM
Hi @EH Desev
It's because of these lines in your script.
if (isLoading || newValue === '') {
return;
}
As you can see, if the new value is empty we return and do nothing.
So you can adjust like below.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
//Separate the new value case is empty
if (newValue === '') {
g_form.clearValue('u_critical_incidents');
return;
}
var userName = new GlideAjax('TaskThree');
userName.addParam('sysparm_name', 'criticalIncidents');
userName.addParam('sysparm_var1', g_form.getValue('business_service'));
userName.getXMLAnswer(callback);
function callback(response) {
var answer = response;
g_form.setValue("u_critical_incidents", answer);
}
}
Cheers,
Tai Vu