Check if business service field is empty and clear another field's value

EH Desev
Tera Contributor

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);

    }

}
1 ACCEPTED SOLUTION

Harish KM
Kilo Patron
Kilo Patron

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);

}

Regards
Harish

View solution in original post

4 REPLIES 4

Vrushali  Kolte
Mega Sage

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.

Harish KM
Kilo Patron
Kilo Patron

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);

}

Regards
Harish

Thanks, Harish! This resolved my problem.

Tai Vu
Kilo Patron
Kilo Patron

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