- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-24-2017 06:17 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-28-2017 12:10 AM
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');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2017 09:38 AM
Hi Rohit,
This can be achieved very easily by following steps,
A Choices :
B Choices :
1. Open the record form, and right click on B field and click on 'Configure Dictionary',
2. In the Dictionary entry, click on 'Advanced View' in the related link.
3. Scroll down and click on 'Choices' tab in the related list section,
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 :
Hope this helps.
Do not feel shy to mark correct or helpful answer if it helps or is correct.
Best Regards,
Nitesh Asthana

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2017 10:54 AM
nitesh_asthana mate i know and i had already proposed this solution. please check my first reply

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2017 11:13 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-26-2017 11:28 AM
nitesh_asthana true mate

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2021 07:16 PM
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!!