- 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-08-2020 04:09 AM
Hi,
Check my updated comment, It is working fine. I have tested in my PDI.
Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-08-2020 04:23 AM
Hi,
the above script is onchange of string field and not fruit type; I updated the previous comment.
the above script should work fine even though you are hiding the fruit type field on form
it will show alert when user types value in string field and that is present in the choices
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 05:57 AM
The script is working only when the user enter the text same as choice value, it is not working if he use upper case. For ex choice value is orange, if user enters ORANGE or Orange it is not working.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2020 04:31 AM
you can use this to handle for uppercase or lowercase
1) while pushing into array convert the values to lowercase
2) also the newvalue make it lower case for easy comparison
Updated 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++) {
var value = sel.options[i].value.toLowerCase().toString();
arr.push(value);
}
newValue = newValue.toLowerCase();
if(arr.indexOf(newValue) >=0){
alert('Found');
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader