- 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 02:35 AM
Hi,
Yes that can possible,
Find below query.
gr.addQuery('value', 'CONTAINS', choiceVal);
Thanks,
Dhananjay.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2020 03:34 AM
Thanks Dhananjay,
It is working for keyword search, but if we give techmahindra it is not showing anything because in choice list the value is like "Tech Mahindra". How to manage these spaces?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2020 04:29 AM
If your question is still unanswered I would encourage to open the question so that people can check and help you in this.
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-14-2020 04:37 AM
Sorry I was in meeting. In this case you can split the value and then compare the value.
Thanks,
Dhananjay.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2020 04:47 AM
You need to update the client script as per below.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var strVal=g_form.getValue('short_description'); //Enter correct field name here
var finalVal=strVal.substring(0,4); //Enter how many first characters you want to compare, I have taken first four
var ga = new GlideAjax('checkChoiceValues');
ga.addParam('sysparm_name','getDetails');
ga.addParam('sysparm_req_for',finalVal);
ga.getXML(callback);
function callback(response)
{
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer==true||answer=='true'){
alert("choice already present");
}
}
}