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

Nitesh Asthana1
Kilo Guru

Hi Rohit,



This can be achieved very easily by following steps,



A Choices :


find_real_file.png


B Choices :


find_real_file.png


1. Open the record form, and right click on B field and click on 'Configure Dictionary',


find_real_file.png


2. In the Dictionary entry, click on 'Advanced View' in the related link.


find_real_file.png


3. Scroll down and click on 'Choices' tab in the related list section,


find_real_file.png


Note: Here 'Dependent Value' refers to A field's value. You can simply specify for what A   field's value which B field's value will be visible.



Output :


find_real_file.png


find_real_file.png



Hope this helps.


Do not feel shy to mark correct or helpful answer if it helps or is correct.



Best Regards,


Nitesh Asthana


nitesh_asthana mate i know and i had already proposed this solution. please check my first reply


harshvardhansingh Yes I had already seen your reply buddy. The link you provided tells how to make a field dependent only. Since he mentioned he is very new to ServiceNow, I elaborated the steps in more details with examples so that he doesn't get confused.



The intent of both of us is to help him achieve what he is looking for. Cheers!!




Best Regards,


Nitesh Asthana


nitesh_asthana true mate


Ankan Mukherje1
Giga Expert

Hello All,

I wrote a simple 'Business Rule'(before or after) on the form table as below(showing the example of 2 fields but it can be extended to any number of fields) and it is working as expected.

 

Syntax

======

(function executeRule(current, previous /*null when async*/) {

var A = current.(Your First Field);
var B = current.(Your Second Field);

if(A == "(give desired choice value of the First Field)" && hosted == "(give desired choice value of the Second Field)"){
current.(Your Result Field) = '(give choice value you want to see)';
} else if(A == "(give desired choice value of the First Field)" && hosted == "(give desired choice value of the Second Field)") {
current.(Your Result Field) = '(give choice value you want to see)';
}else {
current.(Your Result Field) = '(give choice value you want to see)';
}
})(current, previous);

 

Please try it at your end and mark my answer as 'correct answer' if it helps you.

Thanks!!