Dynamic Options in Field Not Triggering UI Policy in ServiceNow

ChetanaP
Tera Contributor

Hi everyone,

I am working on a custom table where I dynamically populate the options of a field (What Needed?) based on the value selected in another field (Request Type). I am using a Client Script for this.

Here’s my Client Script:

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if ( newValue === '') {
      return;
   }
   var whatNeeded = g_form.getValue('u_what_needed');
   g_form.clearOptions('u_what_needed');
   if(newValue == 'hr'){
    g_form.addOption('u_what_needed','hr1','Human Resource 1');
    g_form.addOption('u_what_needed','hr2','Human Resource 2');
    g_form.addOption('u_what_needed','other','Other');
   }
   if(newValue == 'facilities'){
    g_form.addOption('u_what_needed','facilities1','Facilities 1');
    g_form.addOption('u_what_needed','facilities2','Facilities 2');
    g_form.addOption('u_what_needed','other','Other');
   }
   if(newValue == 'legal'){
    g_form.addOption('u_what_needed','legal1','Legal 1');
    g_form.addOption('u_what_needed','legal2','Legal 2');
    g_form.addOption('u_what_needed','other','Other');
   }
   if(isLoading && !g_form.isNewRecord){
    g_form.setValue('u_what_needed', whatNeeded);
   }
   
   //Type appropriate comment here, and begin script below
   
}


After dynamically populating options, I want to:

Show another field (Other Details) and make it mandatory if What Needed? is set to Other.
Use a UI Policy to manage this behavior.
 

Issue:
When I try to create a UI Policy with the condition What Needed? is Other, it doesn't work because the UI Policy doesn't detect the dynamically added options in the What Needed? field.

2 REPLIES 2

JenniferRah
Mega Sage

Create the "Other" option in your field. Since you are clearing the options first, you will not see it unless you add it back in, but then you can use it in the UI Policy.

raj chavan
Tera Guru
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (newValue === '') {
        return;
    }

  
    var whatNeeded = g_form.getValue('u_what_needed');
    g_form.clearOptions('u_what_needed'); // Clear existing options

  
    if (newValue == 'hr') {
        g_form.addOption('u_what_needed', 'hr1', 'Human Resource 1');
        g_form.addOption('u_what_needed', 'hr2', 'Human Resource 2');
        g_form.addOption('u_what_needed', 'other', 'Other');
    } else if (newValue == 'facilities') {
        g_form.addOption('u_what_needed', 'facilities1', 'Facilities 1');
        g_form.addOption('u_what_needed', 'facilities2', 'Facilities 2');
        g_form.addOption('u_what_needed', 'other', 'Other');
    } else if (newValue == 'legal') {
        g_form.addOption('u_what_needed', 'legal1', 'Legal 1');
        g_form.addOption('u_what_needed', 'legal2', 'Legal 2');
        g_form.addOption('u_what_needed', 'other', 'Other');
    }

    // Restore the field value if the form is loading
    if (isLoading && !g_form.isNewRecord) {
        g_form.setValue('u_what_needed', whatNeeded);
    } else {
        // Set the value explicitly to trigger UI Policy
        g_form.setValue('u_what_needed', ''); // Clear value first
        g_form.setValue('u_what_needed', whatNeeded); // Reset the correct value
    }
}

Try this version let me know if it works


If my answer helped you in any way, please then mark it as helpful or correct ✔️. This will help others finding a solution.

Kindly mark it correct and helpful if it is applicable.

Thanks,

Raj