how to add/remove option for reference field based on a field selection

Jasmine1
Kilo Explorer

I have two fields "application"(with 3 values) and another reference field "code".

So based on selection of application (value), I want to control the reference field code to add/remove the value none from the reference table.

I understand that addoption/removeoption only works on choice list.So is there any other way out for this?

This is urgent.

Thank you.

7 REPLIES 7

Hi Jasmine,

Then you can configure your reference field to show as a choice list with none.

And then based on onchange client script of application, you can remove other options if application matches your app.

for others, leave as is.

Ref: https://docs.servicenow.com/administer/field_administration/task/t_DisplayTheRefFieldAsAChoiceList.h...

Mark the comment as a correct answer and also helpful once worked.

Ruhi Jibhyenka1
Mega Guru

Hi Jasmine,

Refer this example, i have two fields location and building, if select a value as Pune in location then only buildings in Pune location should be displayed in building field.

Write a script include make it client callable

Script include:

function buildingdropdown()
{
var gp = ' ';
var tp=current.variables.location;

var u=new GlideRecord('cmn_building');
u.addQuery('location',tp);
u.query();
while(u.next())
{

//gs.addInfoMessage(tp);
gp += (',' + u.sys_id);
}

return 'sys_idIN' + gp;


}

 

Client script: type-onChange, Variable name- select on which you want to change

var ga = new GlideAjax('scriptinclude_name'); 

ga.addParam('sysparm_name','function_name in script include'); 

ga.getXML(dropdown);

function dropdown(response) 

    var answer = response.responseXML.documentElement.getAttribute("answer"); 

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy.

Thanks,

Ruhi.

Taha El alami
Kilo Sage
Kilo Sage

Hi all it's worked form me using :

g_form.removeOption("fildname","choice_sys_id");

 

exmple: 

my fieldname : type;

i have a choice called "abc" i need to remove it from the choice list so i copy the "abc" sys id from the reference table record .

g_form.removeOption("type","3bcdccf9db26d740afa29532ca9619**");