Server side script to verify dropdown field values

Som4
Tera Contributor

I want to create a server side script to verify whether certain category values like x ,y, z are present in the category field dropdown on the sn_hr_core_case table. How can I achieve this?

5 REPLIES 5

Kristen Ankeny
Kilo Sage

You could query the sys_choice table - this captures all of the choices for table columns.

Soumyadeep Dutt
Tera Contributor

Please find the below code for this. If it helps please mark my solution helpful.

 

(function() {
// Initialize variables for category values to check
var categoriesToCheck = ['x', 'y', 'z'];
var categoriesFound = [];

var gr = new GlideRecord('sys_choice');
gr.addQuery('name', 'sn_hr_core_case');
gr.addQuery('element', 'category');
gr.query();
while (gr.next()) {
var value = gr.getValue('value');

// Checking if the current choice value is one of the categories to check
if (categoriesToCheck.includes(value)) {
categoriesFound.push(value);
}
}
return categoriesFound;
})();

Slava Savitsky
Giga Sage

Would you please share some more details about your whole use case? Why do you need this type of check? What are you actually trying to achieve?

Gabriel MF
ServiceNow Employee
ServiceNow Employee

Hi @Som4 !

You can achieve it with following code:

/**
     * Validates if a choice is present in a field of a table.
	 * @param {string} table - table to look in sys_choice reference
	 * @param {string} field - field you want to look for
     * @param {string} value - value you want to verify if exists
	 * @returns {boolean} true if the value is found and false if it's not found
   */
 validateChoice: function(table, field, value) {
        var choices = new GlideRecord('sys_choice');
        choices.addQuery('name', table);
        choices.addQuery('element', field);
        choices.addQuery('value', value);
        choices.query();

        if (!choices.next()) {
            return false;
        }
        return true;
    },

 

Best regards,

Gabriel