How to compare value in string field with choices in other field?

rkreddy
Giga Expert

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

1 ACCEPTED SOLUTION

Dhananjay Pawar
Kilo Sage

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.

View solution in original post

33 REPLIES 33

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?

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.

@rkreddy 

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@rkreddy 

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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.