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.

Get reference field values based on another field

Lemajeur
Mega Expert

Hello,

 

I have a requirement where the catalog item has 2 fields, A and B ( both of type reference), I want to build an onChange script where when a user select a value in A, field B should only populate values that are in relationship with choice in field A.

 

How do I achieve this?  My code is currently failing.

1 ACCEPTED SOLUTION

Shambhu5
Kilo Guru

Hi Leandre,

I too had a similar requirement, in which I had to select the Manager in 1 reference field, and his (selected  manager's) related members only needed to be shown in other Reference Field.

For this, I made use of Reference Qualifier and called them in Script include.

For Manager reference field, I used Advanced reference qualifier, in Reference qualifier javascript:getManager()

Its Script include is as per the screenshot:

find_real_file.png

Similarly, to get members, used Advanced Reference Qualifier, in Reference qualifier javascript:getMember()

Members Script include is as per the screenshot:

find_real_file.png

 

It worked fine for me.

Please mark my answer as Correct or Helpful, if in case it resolved your query.

 

Regards,
Vishrut

find_real_file.png

www.DxSherpa.com

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

View solution in original post

5 REPLIES 5

Shantharao
Kilo Sage

Hi,

 

you can use  var a=g_form.getReference("A"), now you will get entire object of field A then you can set B value from the A object by using 

g_form.setValue("b",a.name)

 

Thamks

Hi Shantharao,

 

I did this, and still it is now working, would you see what's wrong with my code?

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == "") {
      var test = g_form.getReference("smr_model");
       g_form.setValue("smr_sw_discovery_model", smr_model);
   }

   //Type appropriate comment here, and begin script below
   
}

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == "") {
     
   }

   //Type appropriate comment here, and begin script below

var test = g_form.getReference("smr_model"); // from this line u have stored "smr_model"(A) object in "test " variable and you need to set value into B field u can use "test" object dot some field name for ex "test.name" instead of smr_mode
  g_form.setValue("smr_sw_discovery_model", test.name);
   
}

Shambhu5
Kilo Guru

Hi Leandre,

I too had a similar requirement, in which I had to select the Manager in 1 reference field, and his (selected  manager's) related members only needed to be shown in other Reference Field.

For this, I made use of Reference Qualifier and called them in Script include.

For Manager reference field, I used Advanced reference qualifier, in Reference qualifier javascript:getManager()

Its Script include is as per the screenshot:

find_real_file.png

Similarly, to get members, used Advanced Reference Qualifier, in Reference qualifier javascript:getMember()

Members Script include is as per the screenshot:

find_real_file.png

 

It worked fine for me.

Please mark my answer as Correct or Helpful, if in case it resolved your query.

 

Regards,
Vishrut

find_real_file.png

www.DxSherpa.com