How to create new sys_choices from Record Producer Variable
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 09:07 AM
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
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 09:13 AM
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.
Suresh.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 09:16 AM
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();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 09:51 AM
Dan would this be added to the RP script or would I need a script include and client ajax?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2022 09:58 AM
Yes put it into a script include and call it from client script via GlideAjax (instead of GlideRecord)