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

ersureshbe
Giga Sage
Giga Sage

Hi,

Please try below code.but make sure Display value and Database value should be same or not. Based on that you should slightly modify this line 'gr.addQuery('label', choice);'

var choice = producer.yourfieldName;

var gr = new GlideRecord('sys_choice');
gr.addQuery('element', yourFieldNAme);
gr.addQuery('label', choice);
gr.query();
if(!gr.next()){
gr. = choice;
gr.update();
}

Please mark as correct answer if it helped.

Regards,

Suresh.

Regards,
Suresh.

Dan H
Tera Guru

 

Hi, something like this:

var choicesGR = new GlideRecord('sys_choice');

choicesGR.addQuery('label',recordProducer.EVENTNAME);

choicesGR.addQuery('table', table you want to add/check the choice too);

choicesGR.query();

if(!choicesGR.next()){

    var newChoice = new GlideRecord('sys_choice');

    newChoice.newRecord();

    newChoice.name = "Table for choice";

    newChoice.value = "Value of choice record";

    newChoice.label = "label for choice";

    newChoice.language = "language for choice";

    newChoice.element = "element for choice";

    newChoice.insert();

}
 
 

Dan would this be added to the RP script or would I need a script include and client ajax?

 

Yes put it into a script include and call it from client script via GlideAjax (instead of GlideRecord)