Server side script to verify dropdown field values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2024 11:18 AM
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2024 03:38 PM
You could query the sys_choice table - this captures all of the choices for table columns.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-12-2024 10:04 PM
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;
})();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-13-2024 11:40 AM
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?
Blog: https://sys.properties | Telegram: https://t.me/sys_properties | LinkedIn: https://www.linkedin.com/in/slava-savitsky/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-13-2024 01:38 PM
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