Create a new Choice Option On GlideRecord Insert

whitewalker
Kilo Contributor

I create a script that insert new records in a table. One of table fields is a Choice List. How can I check on insert if the desired value for Choice List field exists, and create a new Choice Option if not ?

1 ACCEPTED SOLUTION

Ravi Prasad1
Tera Guru

Hope below script will help you !!!



var gr=new GlideRecord('sys_choice');


gr.addQuery('name',"table_name");


gr.addQuery('element',"column_name");


gr.addQuery('value',"value");


gr.query();


if(!gr.next())


{


var gr1=new GlideRecord('sys_choice');


gr1.initialize();


gr1.name="table_name";


gr1.element="column_name";


gr1.value="value";


gr1.label="label";


gr1.language="En";


gr1.insert();




}


View solution in original post

4 REPLIES 4

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You should be able to query the sys_choice table (all choices in the system are in that table) for the record corresponding to the specific table, element, and value you're looking for and create it if your query doesn't return any records.


Ravi Prasad1
Tera Guru

Hope below script will help you !!!



var gr=new GlideRecord('sys_choice');


gr.addQuery('name',"table_name");


gr.addQuery('element',"column_name");


gr.addQuery('value',"value");


gr.query();


if(!gr.next())


{


var gr1=new GlideRecord('sys_choice');


gr1.initialize();


gr1.name="table_name";


gr1.element="column_name";


gr1.value="value";


gr1.label="label";


gr1.language="En";


gr1.insert();




}


Thank you very much to both of you.


Hello,

Following to this post I'm sort of stuck. I have a choice list field domain with option (Science,Commerce & Arts) and for every domain I have 2 options which includes "other" as second option.

For an example if I'm choosing domain as science then I will have 2 options one is Biology and second is other. If I select "other" option  the string field appear which asks me to enter my answer, once I enter it should go and check in choice table with dependent field science and if that choice dis not available then  it should create a choice there.