available choices in choice list

Dominik1988
Tera Expert

Hi, I have field choice list on the form and some choices are removed from this field by the script. I want to review  which choices are currently available. I want to get access to this field from background scripti using glide record. Can you help me with provideing any script template for this? 

Thank you in advance! 🙂

3 REPLIES 3

Sid_Takali
Kilo Patron

Hi @Dominik1988 this kind of script you can write 

var sysChoice = new GlideRecord("sys_choice");
sysChoice.addQuery("name", "incident");
sysChoice.addQuery("element", "state");
sysChoice.addQuery("language", "en");
sysChoice.orderBy("sequence");
sysChoice.query();
var table, element, label, infos = [];
while (sysChoice.next()) {
    gs.print("sequence=" + sysChoice.sequence + "; value=" + sysChoice.value + "; label=" + sysChoice.label);
}

Please mark my answer Correct/Helpful

Regards,

Siddharam

Maddysunil
Kilo Sage

@Dominik1988 

You can use below example script to get the available choice

   // Replace 'your_choice_list_field' with the actual choice list field name
    var choiceListField = 'your_choice_list_field';

    var gr = new GlideRecord('sys_choice');
   gr.addQuery('your_condition_field', 'your_condition_value');
    gr.query();
    while (gr.next()) {
        var availableChoices = gr.getElement('your_choice_list_field').getChoices();
        gs.info('Record ' + gr.getDisplayValue() + ' - Available Choices: ' + availableChoices.join(', '));
}

 

Please mark my answer Correct/Helpful

Thanks

Dominik1988
Tera Expert

Actually exact problem is that I want to get all choice list values from scrachpad. my field is located on a form "device" and there is status field on this form. there is also client script which take all available state values from the scratchpad.  I want to previewed those values  and problem is that I need to do this from background cript.

My Idea is:

 
Spoiler
var gr = new GlideRecord('cmdb_ci_my_device');
gr.get('sys_id_of_my_device_form');
 
 // this I know is wrong but I can't find how to get values from the scrachpad on this exact form
var States = gr.g_scratchpad.choiceValuesList.split(",");
 
//here I want to print result as string
gs.print(States);