How to set a value based on a reference field

Sai vsk
Tera Expert

Hi,

We have a reference field(ex: sys_user). Based on that reference table, we are populating details in other field. Now if the user doesn't have mobile number in User table we are setting that field value as NA. If the user has mobile number then it will populate in the field. Now the script is fine if the user is having mobile number but not setting to NA if mobile number is empty. This is a simple script but not sure why it is not working.

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var test =g_form.getReference("name_of_the_student",user);
function user(test){
var mob= "NA";
if(test.mobile_phone!=" ")
g_form.setValue("mobile_number",test.mobile_phone);
else if(test.mobile_phone==" ")
g_form.setValue("mobile_number",mob);
}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Sai vsk 

update as this

you should be comparing like this to check if it has value

if(test.mobile_phone)

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var test = g_form.getReference("name_of_the_student",user);
	function user(test){
		var mob = "NA";
		if(test.mobile_phone!="")
			g_form.setValue("mobile_number",test.mobile_phone);
		else
			g_form.setValue("mobile_number",mob);
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Sai vsk 

update as this

you should be comparing like this to check if it has value

if(test.mobile_phone)

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var test = g_form.getReference("name_of_the_student",user);
	function user(test){
		var mob = "NA";
		if(test.mobile_phone!="")
			g_form.setValue("mobile_number",test.mobile_phone);
		else
			g_form.setValue("mobile_number",mob);
	}
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks @Ankur Bawiskar .

Can you just tell me the difference between the script you shared and my script.