onChange Client script - clear dependent field when user clears IP address field

BiancaK
Tera Expert

Good Day fellow Devs

 

I am performing a field validation on the IP address field and thereafter I am setting the IP address value to another field (machine name field). 

After setting the IP address field value to machine name field, if the user clears the value in the IP address field the machine name field is not cleared. How do I clear it please?

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
 
    var ip = g_form.getValue('ip_address');
 
    var reg = /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/;
 
    if (!reg.test(ip)) {
        alert('Please enter correct IP Address');
        g_form.clearValue('ip_address', '');
    } else {
        var machineName = ip + ".abc.xxxzzz.corp";
        g_form.setValue("machine_name", machineName);
    }
}
 
BiancaK_0-1690068157352.png

 

I have tried using g_form.clearValue(); but it does not work
 
BiancaK_0-1690068243207.png

 

1 ACCEPTED SOLUTION

Sagar Pagar
Tera Patron

Hi @BiancaK,

 

You need to use the clearValue() ot setValue() with null value for machine name variable. You can try this updated scripts.

UI type: All

 

 

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

		g_form.clearValue("machine_name");
		return;
	}

	var ip = g_form.getValue('ip_address');

	var reg = /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/;

	if (!reg.test(ip)) {
		alert('Please enter correct IP Address');
		g_form.clearValue('ip_address');
	} else {
		var machineName = ip + ".abc.xxxzzz.corp";
		g_form.setValue("machine_name", machineName);
	}
}

 


If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

The world works with ServiceNow

View solution in original post

5 REPLIES 5

DanielCordick
Mega Patron
Mega Patron

Do you have the UI type set to All?

Sagar Pagar
Tera Patron

Hi @BiancaK,

 

You need to use the clearValue() ot setValue() with null value for machine name variable. You can try this updated scripts.

UI type: All

 

 

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

		g_form.clearValue("machine_name");
		return;
	}

	var ip = g_form.getValue('ip_address');

	var reg = /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/;

	if (!reg.test(ip)) {
		alert('Please enter correct IP Address');
		g_form.clearValue('ip_address');
	} else {
		var machineName = ip + ".abc.xxxzzz.corp";
		g_form.setValue("machine_name", machineName);
	}
}

 


If my response helps to solve your issue. Kindly mark it as helpful & correct. It will be helpful for future readers.
Thanks,
Sagar Pagar

The world works with ServiceNow

Thank you @Sage 

Hi @Sagar Pagar 

For my learning purposes this raises a greater question. 

Is it possible to try and figure out whether a field is null by assigning the null value to a variable because it seems like it is impossible. 

Whenever, a field value is null and I try to store it to a variable and run a conditional statement on it then it fails. 

So it seems then that it can only work when field values has been changed to empty or null using the above code.

 

This seems different to main stream languages like Java where you can store empty field values in a string variable and then even get the length of it which will be 0

 

Your assistance will be appreciated.