- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2020 03:04 AM
Hi Everyone,
I just want to know whether the following thing is possible or not.
I have a choices like apple,mango,orange,banana in "fruit type" choice field and I have a string field "fruit name". When user enters value in string field, it needs to compare with choices if the value already exists in choices then a info message or alert should be populated like Fruit already exists. For example if user enters apple in string field, as Apple already there in choices the alert should be populated. If user enters pineapple, then as pineapple is not there in choices, it should be added as a choice to that choice field.
Thank you.
Rakesh
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2020 03:27 AM
Hi,
In this case you can write onChange client script on string field.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('checkChoiceValues');
ga.addParam('sysparm_name','getDetails');
ga.addParam('sysparm_choiceVal',newValue); //newValue will requester for
ga.getXML(callback);
function callback(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer==true||answer=='true'){
alert("choice already present");
}
}
}
Create script include
Script include name - checkChoiceValues
client callable - true
var checkChoiceValues = Class.create();
checkChoiceValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDetails :function(){
var choiceVal=this.getParameter('sysparm_choiceVal');
var gr=new GlideRecord('sys_choice');
gr.addQuery('value',choiceVal);
gr.query();
if(gr.next())
{
return true;
}
else{
return false;
}
},
type: 'checkChoiceValues'
});
Thanks,
Dhananjay.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2020 06:49 AM
No problem Dhananjay, you are still replying that means a lot. Thanks for the patience. I have a doubt that, I don't have particular number of characters it varies from string to string, in this case how can we do?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2020 06:58 AM
Hi,
Though it varies but for comparison it will take first four and will compare and will return the result.
I have tested this and working fine.
e.g I am having integration testing choice value and if I enter continuous value like integrationtesting but it will take only inte and will search for it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2020 04:44 AM
Just out of curiosity is this a business use-case/requirement to create new choice via script?
Because I haven't come across any such requirement wherein you are required to create choices from script.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2020 03:59 AM
Is this for catalog item or table form?
This would work for table form and field.
onChange on string field
Ideally you should not add choice from script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var sel = g_form.getElement('fruit_type'); // use field name here
var arr = [];
for (var i=0, n=sel.options.length;i<n;i++) {
arr.push(sel.options[i].value.toString());
}
if(arr.indexOf(newValue) >=0){
alert('Found');
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2020 04:07 AM
Thanks Ankur for the answer. But fruit type is choice field, I hided this on the form to restrict the user from selecting the choices in it. Only string field could be visible to user and whenever the user enters any value in string field it should be compared with the list of choices in choice field. If any matching value found then need to get alert.