Set value for reference field as a choice list

SamuelTse
Tera Guru

HI,

I have a reference field and I have it set up as a choice list (drop down list).

find_real_file.png

I am trying to set the value of this field via client script. I can set the value of the reference field. However, the display value is not selecting.

I have used to set the value for the reference field just fine so it is not a problem of the values

setValue(String fieldName, String value, String displayValue)

However, once i changed it to be a choice list, by selecting "Dropdown with -- None -- " in the dictionary, the script no longer works.

Any idea how to set the value of a reference field when it is displayed in a choice list format?

Thanks,

Sam

5 REPLIES 5

Mr world wide
Mega Guru

Hi @SamuelTse 

 

Based on the Documentation setValue method takes only 2 parameters.

setValue(String fieldName, String value)

Check out this link for more info:  https://www.oreilly.com/library/view/learning-servicenow/9781785883323/a86a5275-3be1-4c44-8283-7f1df...

If the "String value" matches with the choice list it will get selected automatically. (If it won't match with the existing choices it creates a temporary choice only to that record, Which will be in Blue color)

  

Please find the screenshot for reference:

find_real_file.png

 

Note: These Blue color choices will not be available on the sys_choice table. (it is only for that specific record)

Yes. If this were a choice field type, that will work. However, the field I am trying to set is a reference field. This is what I did to have it as a choice list, instead of having magnifying glass on the field:

https://docs.servicenow.com/bundle/paris-platform-administration/page/administer/field-administration/task/t_DisplayTheRefFieldAsAChoiceList.html

Setting a value to a reference field requires three parameters in the SetValue function. This has been working for me for a normal reference field:

https://developer.servicenow.com/dev.do#!/reference/api/orlando/client/c_GlideFormAPI#r_GlideFormSetValue_String_String_String?navFilter=setvalue

I need help to set the value when the reference field is in the choice list format. Being a reference field, the value being set has to be a sys id.

Thanks for the quick reply

Sam

 

Got it!

Instead of "String value", you need to send the "sys_id" of reference record as a "String value".

 

setValue('category', '23198424db01a050125e138b4b9619b4')

//1st parameter is field backend name
//2ns parameter is sys_id of the reference record.
 

Hi Samuel,

         can i have piece of code for this - need to send the "sys_id" of reference record as a "String value"