How to create new sys_choices from Record Producer Variable

triciav
Kilo Sage

I have a Record Producer with a String Field for Event Name

I need to check the sys_choice table to see if this is already a choice and if note create it in the sys_choice table.

Can someone share code on how to achieve this please.

Thank you

8 REPLIES 8

Anurag Tripathi
Mega Patron
Mega Patron

Hi,

Best way would be to add an onchange client script that runs validates it

   var ga = new GlideAjax('ChoiceAvailable');
      ga.addParam('sysparm_name', 'ChoiceAvailable');
      ga.addParam('sysparm_field', g_form.getValue("state"));
ga.addParam('sysparm_table', g_form.getValue("incident"));
      ga.addParam('sysparm_fiedlLabel', g_form.getValue("new"));
      ga.getXML(getresponse);
}


function getresponse(response) {
      var answer = response.responseXML.documentElement.getAttribute("answer");

   if(answer )
{
g_form.clearValue('<variable>')
}
 }

 

Script include

var ChoiceAvailable = Class.create();
ChoiceAvailable.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    checkChopice: function() {
        var fiedl = this.getParameter('sysparm_field');
        var choice_label = this.getParameter('sysparm_fiedlLabel');
		var field_table = this.getParameter('sysparm_table');

        var gr = new GlideRecord('sys_choice');
        gr.addQuery('element', fiedl);
        gr.addQuery('label', choice_label);
		 gr.addQuery('name', field_table);
        gr.query();
        if (gr.hasNext()) {
            return true;
        }
    },
    type: 'ChoiceAvailable'
});
-Anurag

Anurug,

This looks like the way I was trying to build it.

 

Question I need to create a choice if one does not exist

so in the script include I would need to do an initialize?

 

This is just to validate whether this will be a unique entry or not. 

Choice creation would happen on submission of record producer, that is what the record producer is for, right?

-Anurag

No this is to create a record on a custom table, the custom table has a choice field that if the choice does not already exist create it. 

The variable is a string

so a user enters the event name

GA lookup to sys_choice

if it does not find the choice create it in the sys_choice table so on the table UI for the records it has the list of choices