- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 04:21 AM
Hi Guys!
I'm stuck on this script as I'm not sure how to populate the field.
I have 3 fields. Depending on the value of two of them, the third one should only have certain choices available.
For example, if someone selects "Brazil" as the country (Field 1) and "Sust. Energy" as the business unit (Field 2), then the available options for the "Business Work Group" (field 3) should be "Projects".
Similarly, if "Spain" is selected as the country and "Enablers" is selected as the business unit, then options for the "Business Work Group" field should include "Retail", "Marketing", "Digital", and so on.
I am doing a client script but i cant seem to make it work.
For reference,
Field 1 is a choice list field
Field 2 is also a Reference field
Field 3 is a reference field that is in the Groups Table.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get references to the form elements
var countryField = g_form.getValue('u_country');
var businessUnitField = g_form.getValue('business_unit');
var businessWorkGroupField = g_form.getValue('u_business_work_group');
//Type appropriate comment here, and begin script below
if (countryField == 'Brazil' && businessUnitField == 'Sust.Energy') {
businessWorkGroupField.setDisplay('Projects');
} else {
businessWorkGroupField.setDisplay(' ');
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 05:51 AM
Don't use a client script. Refer to a script include function in the Business Work Group variable reference qualifier. Pass as parameters the chosen values of the other two fields, and return the sys-ids of the valid choices.
You can use this as example:
Solved: Call Script include from Reference Qualifier - ServiceNow Community
And maybe use a ui policy to set the Business Work Group to read only until the other 2 fields are chosen.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 04:38 AM
Try using "getDisplayValue()" instead for reference fields , also try setValue() inside if body.
please mark my solution as accepted if it helped, also mark it helpful
regards,
Ubada Barmawar.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 05:18 AM
Please try with below updated code:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get references to the form elements : use g_form.getDisplayBox('field_name');
var countryField = g_form.getValue('u_country');
var businessUnitField = g_form.getDisplayBox('business_unit');
var businessWorkGroupField = g_form.getDisplayBox('u_business_work_group');
//Type appropriate comment here, and begin script below
if (countryField == 'Brazil' && businessUnitField == 'Sust.Energy') {
businessWorkGroupField.setDisplay('Projects');
} else {
businessWorkGroupField.setDisplay(' ');
}
}
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 05:36 AM - edited 04-19-2024 05:38 AM
Hi @NMMZ10 ,
Please try below script
var countryField = g_form.getValue('u_country');
var businessUnitField = g_form.getValue('business_unit');
var businessWorkGroupField = g_form.getValue('u_business_work_group');
//Type appropriate comment here, and begin script below
if (countryField == 'Brazil' && businessUnitField == <use sysId for that part>) {
g_form.setValue('u_business_work_group, 'sys_id of project');
g_form.update()
} else {
g_form.setValue('u_business_work_group, 'sys_id of project');
}
Please mark my answer correct and helpful if this works for you
Thanks and Regards
Sarthak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2024 05:51 AM
Don't use a client script. Refer to a script include function in the Business Work Group variable reference qualifier. Pass as parameters the chosen values of the other two fields, and return the sys-ids of the valid choices.
You can use this as example:
Solved: Call Script include from Reference Qualifier - ServiceNow Community
And maybe use a ui policy to set the Business Work Group to read only until the other 2 fields are chosen.