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

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.

Hi Dhananjay,

Thanks for the reply. This is the thing I need, but in string user enters the value in different formats, then how to convert it to lower case that matches with choice value. And also if choice is not there, then how to add this string value to the choice list of that choice field.

Hi,

Can you explain on "user enters the value in different formats" So I can understand.

And about your second point, creation of choice

Find below updated script include code.

 

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{
var gr1 = new GlideRecord('sys_choice');
gr1.initialize();
gr1.name='incident'; // Enter correct table name on which choice field is present(here I have designed this for incident that is why I habe mentioned incident table name)
gr1.element='u_fruit_type'; // enter correct choice field name
gr1.language='en'; // this is for language
gr1.label=cat_user; // choice label
gr1.value=cat_user; // choice value
gr1.insert();
}

},

type: 'checkChoiceValues'
});

 

Can you explain on "user enters the value in different formats"? -- I mean user may enter in upper case or lower case or camel case even, but choice value is in lower case, so whatever the format user enters, it should be converted to lower case.

For this you can use .toLowerCase()

Replace below updated line

gr1.value=cat_user.toLowerCase();

Thanks,

Dhananjay.