Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to get two reference field in client script?

PhoenixMing0912
Giga Expert

Hi all,

For example, A and B are reference fields of incident. I want to compare A.a and B.b in client script, how can I do that?

I know g_form.getReference() but looks like only one reference field can be fetched.

 

Regerds,
Eric

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Hi,

Assuming you want to compare some other fields based on the reference field of A and B, you can do liek this.

var a = g_form.getReference("A"); //put correct field names

var b = g_form.getReference("B"); //put correct field names

if(a.your_field == b.your_field) {

//do something.

}

Mark the comment as a correct answer if it solves your problem.

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Assuming you want to compare if same users are selected in both the fields

you can use this logic

var a = g_form.getValue('field A');

var b = g_form.getValue('field B');

if(a==b){

alert('both users are same');

}

Regards
Ankur

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

asifnoor
Kilo Patron

Hi,

Assuming you want to compare some other fields based on the reference field of A and B, you can do liek this.

var a = g_form.getReference("A"); //put correct field names

var b = g_form.getReference("B"); //put correct field names

if(a.your_field == b.your_field) {

//do something.

}

Mark the comment as a correct answer if it solves your problem.

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
   
return;
   }

   var lm=g_form.getReference('line_manager');
    var dm=g_form.getReference('director_approval');
   var gm=g_form.getReference('general_approval');

   if(lm.employee_number == dm.employee_number || lm.employee_number==gm.employee_number || dm.employee_number==gm.employee_number){
   g_form.clearValue('line_manager');
    g_form.clearValue('director_approval');
      g_form.clearValue('general_approvals');
   }   
}
I tried this it is showing the javascript error

Chander Bhusha1
Tera Guru

Hi Phonexi,

Its working fine for me If you use g_form.getReference in two fields :

client scritp:

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

	var a = g_form.getReference('caller_id');
	var b = a.email;
	var c = g_form.getReference('assigned_to')
	var d = c.name;
	alert('caller email '+b + '   Assigned to name '+ d);

//if(a.email == c.email)  use any condition
	
   //Type appropriate comment here, and begin script below
   
}

 

OUtput:

find_real_file.png

 

 

You can compare with both the values:

 

Mark helpful and correct if it helps.

Thansk,

CB