Auto select the choice field depending on the value of other choice field

rohitservicenow
Mega Guru

Hello,

I am pretty new to ServiceNow need help with the sample code to auto select a choice field based on the choices selected in another field.

Suppose, I have two custom fields, A and B

A has some 50 choices and B has 10. Based on the choices selected on A, I want to auto populate the B's values.

Can we write the OnChange script / Bus rule?

Thanks,

Rohit

1 ACCEPTED SOLUTION

Please try in this way then, make sure different arrays do not have same values.



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


    if (isLoading || newValue === '') {


          return;


    }


var arrayA1 = [1, 10, 15, 20, 24, 25];


var arrayA2 = [2, 6, 7, 8, 38, 49];


    //Type appropriate comment here, and begin script below


if(arrayA1.includes(parseInt(g_form.getValue('field_A'))))


g_form.setValue('field_B', '60');


if(arrayA2.includes(parseInt(g_form.getValue('field_A'))))


g_form.setValue('field_B', '70');


}


View solution in original post

19 REPLIES 19

Harsh Vardhan
Giga Patron

if you are on form then you don't need to write script.


Please refer the link below.



https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/administer/field_administration/t...



if you have any question please let me know


thank you Harsh for the response, but i m not looking to make the field dependent, instead want to auto populate the field. example, I have a field called A, which some choices from 1 to 50 and B has 60,70,80,90,100. whenever user chooses any one of 1, 15,20.24,30,35,48 i want to auto populate the B with 60. Similarly 70,80,90,100 depending on the user's selection of A field.


thanks


You can have onChange() client script on field A to auto populate the field B value using GlideAjax, below sample code might help you. Please update the field names and try, let's see if this helps.



onChange() client script:


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


if (isLoading || newValue === '') {


return;


}


var ga = new GlideAjax('getAjax');


ga.addParam('sysparm_name','getValue');


ga.addParam('sysparm_valA', newValue);


ga.getXML(AsyncCall);


}


function AsyncCall(response) {


var answer = response.responseXML.documentElement.getAttribute("answer");


// g_form.addInfoMessage(answer);


g_form.setValue('b_field_name', answer);


}



Client callable Script Include:


var getAjax = Class.create();


getAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {


getValue : function()


{


var gr = new GlideRecord('table name');


gr.addQuery('<Field name which hold value of A>', this.getParameter('sysparm_valA'));


gr.query();


if(gr.next())


return gr.getValue('<Field name which hold value of A>');


},


      type: 'getAjax'


});


thank you Sishir for the code, however we haven't defined the relationship between the fields. Had the field been the reference field, the relationship might have been defined in the reference table. Since these are the choice fields, where can we define the relationship? If we define the relationship in the script, it's going to be very tiring/monotonous for defining the relationships for 50 odd choices.


example: populate field B with 60 when the value of A is one of 1, 15,20.24,30,35,48?



Thank you.