How to add values in Choice Tables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2023 05:14 AM - edited 02-14-2023 04:31 AM
Hello Community Experts,
We have record producer on the Problem table and one of the Variable called Select Service it's single line text and We are looking for the way to check entered value in select service variable available or not in the sys_choice table and if it's not available we want to create in the choice table is there any way to achive this on submission.
Highly appreciated if anyone share the script to achieve this.
Thanks,
Learner
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2023 05:40 AM
Hello @Learner10 ,
You can achieve this by using a client callable script include and an onSubmit catalog client script. Please find below scripts for your reference:
*Assuming that the element (field) is Service choice field on Incident table
Script Include:
checkChoice :function() {
var service = this.getParameter('sysparm_service');
var choiceGr = new GlideRecord('sys_choice');
choiceGr.addQuery('name','incident'); //table name
choiceGr.addQuery('element','u_service'); //field name
choiceGr.addQuery('value',service); //value to search
choiceGr.query();
if(!choiceGr.next()){
choiceGr.initialize();
choiceGr.element = "u_service";
choiceGr.name = "incident";
choiceGr.value = service;
choiceGr.label = service;
choiceGr.insert();
return "insert";
}
else
return "exist";
}
Catalog Client Script:
var ga = new GlideAjax('CheckChoiceValue'); //script include to call
ga.addParam('sysparm_name','checkChoice'); //function to call
ga.addParam('sysparm_service',g_form.getValue('service')); //select service available variable name
ga.getXMLAnswer(checkValue);
function checkValue(answer){
if(answer == 'insert')
g_form.addInfoMessage("New Service Added!");
}
If my answer has helped with your question, please mark it as helpful and give it a thumbs up!
Regards,
Shubham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2023 06:45 AM
How to give choice number randomly??
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-11-2023 07:05 AM - edited 02-11-2023 07:06 AM
Hello @Learner10 ,
If you want to add sequence for the choice value, instead of adding it randomly you can use below code to increment the sequence based on last choice present for that element.
Try adding below mentioned script at first line inside if block of the script include:
var choiceGr2 = new GlideRecord("sys_choice");
choiceGr2.addQuery('name', 'incident'); //table name
choiceGr2.addQuery('element', 'u_service'); //field name
choiceGr2.orderByDesc('sequence');
choiceGr2.setLimit(1);
choiceGr2.query();
if(choiceGr2.next())
choiceGr.sequence = choiceGr2.sequence + 10; //increment choice sequence by 10 each time
else
choiceGr.sequence = 10; //if this is first choice to be added then set sequence to 10
If my answer has helped with your question, please mark it as helpful and give it a thumbs up!
Regards,
Shubham
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2023 04:23 AM
Hello Shubham,
Sequence not working every addition of the choice it's showing sequence 10