Set dropdown to existing question choices

snexplorer
Tera Contributor

Hello! I'm relatively new to ServiceNow and I'm having some trouble figuring this problem out. Basically, I want to use a query parameter that's passed in the URL to populate an existing select box/dropdown on a catalog item form. I've been able to achieve this with the following:

 

 var params = getUrlParams();
var field= params['field_name'];
if (field) {
            g_form.setValue('field_name', field);
            g_form.setVisible('field_name', true);
}

 

My issue is that the select box variable already has 7 pre-determined question choices. However, my implementation just takes whatever query parameter is passed and populates the select box/dropdown regardless of whether that is a valid, existing choice. This has potential security issues so I've been trying to figure out ways to validate that the choice is valid before setting the value in the form. Any recommendations how I can do this?
 
I've tried a few different routes already including using g_form.getControl('field_name').options or
g_form.getElement('field_name').options and looping over the array. Neither route works because this is apparently the output of getControl:aditidebnat_0-1725481405136.png

According to the output from getControl, the select box has no options or values even though on the UI, the dropdown certainly has choices and I'm able to successfully submit the form. I've tried using g_form.addOption to add new options and see if the output from getControl changes but it doesn't (I still see that options is an array with length 0). 

 

Please let me know if my understanding of any of these methods are wrong or if there is a better way to go about this. Thank you!

 
1 ACCEPTED SOLUTION

JPing
Tera Expert

I don't know how you would be able to do this is front end if the options are not able to appear for you to compare with. So what if you tried to do the comparison on the backend? Use an AJAX call to get to the server side then do a glide record looking in the Question Choice Table <question_choice>. Pass the "question" as the search key for the Glide Record Query along with the url parm value to compare against. The only issue that I can think of right now is that you might have issue if the same Question is asked in a different form - you would need to find some way to ID the right question at a sys_id level or in combination with the item name.

 

 

View solution in original post

2 REPLIES 2

JPing
Tera Expert

I don't know how you would be able to do this is front end if the options are not able to appear for you to compare with. So what if you tried to do the comparison on the backend? Use an AJAX call to get to the server side then do a glide record looking in the Question Choice Table <question_choice>. Pass the "question" as the search key for the Glide Record Query along with the url parm value to compare against. The only issue that I can think of right now is that you might have issue if the same Question is asked in a different form - you would need to find some way to ID the right question at a sys_id level or in combination with the item name.

 

 

RikV
Tera Expert

Hi there,

 

This should work:

 

var params = getUrlParams();
var field = params['field_name'];

if (field) {
    // Get the form element (HTML) for the select box
    var formElement = g_form.getControl('field_name');
    var isValid = false;

    // Loop through the available options within the select element
    for (var i = 0; i < formElement.options.length; i++) {
        if (formElement.options[i].value === field) {
            isValid = true;
            break;
        }
    }

    // Only set the value if it's valid
    if (isValid) {
        g_form.setValue('field_name', field);
        g_form.setVisible('field_name', true);
    } else {
        console.log("Invalid choice passed in the URL parameter");
    }
}