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

Jaspal Singh
Mega Patron
Mega Patron

Yes, you can something as below should help.

var choiceis=choicevalue.toString();

if(choiceis==stringis)

{

}

Conversion of choice value to .toString() would help.

Where choicevalue is suppose variable/field name that has choices.

Hi Jaspal,

Thanks for the answer. But my requirement is, the string value to be compared with all the choices in choice field. If string value matches with any of the choice values, then a alert should be populated.

Sajilal
Mega Sage

Hello,

Maybe you could compare as below on server side,

if(current.string_field == current.choicefield.getDisplayValue())

{

//your info message here

}

In client side, you can use g_form.getValue() method for both the fields and store it in a variable to begin with. and then compare the two variables.

Hope this helps!

Please Mark as Correct if this solves your issue and also mark ???? Helpful if it helps resolve your problem.

Thanks,
Saji