AddOption issue
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 07:35 AM
Hi SN expert,
I need to dynamically set the value of the "Access Type" field based on the "Existing Access" field. The "Access Type" field has two options:
1. New Access
2. Modify Access
If the "Existing Access" field (a multi-line text field) contains any value, the "Access Type" field should automatically be set to Modify Access. If the "Existing Access" field is empty, the "Access Type" field should be set to New Access.
The "Existing Access" field is populated dynamically based on the selection made in the "Module" field (a dropdown with pre-defined choices). When the user selects an option in the "Module" field, a GlideAjax call fetches the corresponding value for "Existing Access" from the database. Depending on whether the fetched value is empty or not, the "Access Type" field should be updated accordingly.
I am using the below script, but it doesn't work also whenever I am changing the module field selection, option is stuck with new access.
This is running on module field (on change)
Kindly assist on this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 07:47 AM
I hope you are giving the correct choice value, instead of adding, remove the option
function onChange(control, oldValue, newValue, isLoading) {
var accessType = g_form.getValue('access_type');
var module = g_form.getValue('module');
var currentAccess = g_form.getValue('existing_access');
g_form.clearOptions('access_type');
if (!currentAccess || currentAccess.trim() == '') {
g_form.removeOption('access_type', '2');
} else {
g_form.removeOption('access_type', '1',);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 08:05 AM
Hi @Ankur Bawiskar
Have tried the remove option too but doesn't work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 07:05 PM
should work ideally
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2025 08:55 AM
Hi @PK14 ,
One possible cause of this issue could be that the value of "Existing Access" is being set dynamically through an AJAX call. When you're checking the value and adding the options, it's possible that the AJAX call hasn't completed yet. This may explain why the option is stuck on "New Access" — as the response from the AJAX call hasn't been received by the time you're checking the value of "Existing Access." A better approach would be to add the options only after receiving the response from the Script Include.
If my answer solves your issue, please mark it as Accepted ✔️ & Helpful👍!