how to set reference field choice options dynamically using client script in servicenow

Sumanth34
Kilo Guru

In a scenario, where in you have a reference field and a reference qualifier condition is already set.

 

Now, what if you have a requirement to modify the results of the reference field based on form change or depending on another field present on the same form?

 

 

1 ACCEPTED SOLUTION

Sumanth34
Kilo Guru

this is achievable with an on-change client script, if you have to glide record to obtain certain records, then create a script include and use glide ajax to call the SI.

 

g_form.addOption() doesn't work for reference fields.

 

but g_form.removeOption() will work if you want to remove certain options from the reference fields.

 

replacement for g_form.addOption() could be g_form.setValue('field','sys_id');

 

if you have to display multiple options under these reference fields, then you can simply create a for loop and use g_form.setValue('field', response[i]);

 

advantage of using g_form.setValue() is it will help in adding multiple field choices on a reference field.

 

ex: 

g_form.setValue('type', 'sysid1');

g_form.setValue('type', 'sysid2');

 

for(var i=0;i<length;i++)

  g_form.setValue('type', response[i]);

 

Hope it is clear, please let me know if you're unable to achieve this.

 

Thanks,

Sumanth

 

 

 

 

View solution in original post

1 REPLY 1

Sumanth34
Kilo Guru

this is achievable with an on-change client script, if you have to glide record to obtain certain records, then create a script include and use glide ajax to call the SI.

 

g_form.addOption() doesn't work for reference fields.

 

but g_form.removeOption() will work if you want to remove certain options from the reference fields.

 

replacement for g_form.addOption() could be g_form.setValue('field','sys_id');

 

if you have to display multiple options under these reference fields, then you can simply create a for loop and use g_form.setValue('field', response[i]);

 

advantage of using g_form.setValue() is it will help in adding multiple field choices on a reference field.

 

ex: 

g_form.setValue('type', 'sysid1');

g_form.setValue('type', 'sysid2');

 

for(var i=0;i<length;i++)

  g_form.setValue('type', response[i]);

 

Hope it is clear, please let me know if you're unable to achieve this.

 

Thanks,

Sumanth