AddOption issue

PK14
Kilo Guru

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)

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.addOption('access_type', '1', 'New Access');
    } else {
           g_form.addOption('access_type', '2', 'Modify Access');
    }
}

Kindly assist on this. 

 

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@PK14 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar 

Have tried the remove option too but doesn't work. 

 

@PK14 

should work ideally

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Vrushali  Kolte
Mega Sage

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👍!