Set reference field from ajax call in catalog client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 03:07 PM
Hello,
I am trying to set the support group of a reference field from a script include that searches the configuration item for its support group. (Support group of CI = Support Group field on catalog item)
Attached are the client scripts, script include and sys property being used.
The script include is returning the correct group sys_id, but the support group will not set for some reason. Any reason why this happens?
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 03:14 PM
I dont see any issue. The only thing I would suggest is add an alert('++++answer+++++'+answer) in line 46 to identify, if you are getting the right value in the answer variable.
Please mark this response as correct or helpful if it assisted you with your question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 05:25 PM
Add the following to your return statements in your Script Include:
JSON.stringify(variable);
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-26-2019 06:18 PM
Hey Jalen,
I would suggest using the setValue(String fieldName, String value, String displayValue) method. To return the sys_id and display value of the support group to the Client Script, you can create an object and then return the object as JSON string.
For example, in script include declare the result object first
var supGroup = {};
then when the ci's support_group is found,
supGroup.sysid = gr.support_group;
supGroup.dispValue = gr.support_group.getDisplayValue();
When it's time to return the result, use
return JSON.stringify(supGroup);
In the Client Script call back function:
after assigning the result to variable "answer", parse the JSON string
var jsonAnswer = JSON.parse(answer);
Then g_form.setvalue('support_group', jsonAnswer.sysid, jsonAnswer.dispValue);
setValue(String fieldName, String value, String displayValue)
Sets the value and display value of the specified field.
When defining a value in a choice list, be sure to use number value rather than the label.
To improve performance by preventing a round trip when setting the value for a reference field, use this method not setValue(fieldName, value). When setting multiple reference values for a list collector field, arrays can be passed in the second and third parameters.
Name | Type | Description |
---|---|---|
fieldName | String | Name of the field. |
value | String | System ID for the reference value in the database. Can be an array of system IDs if the field is a glide-list. |
displayValue | String | Display name for the referenced value in the database. Can be an array of display names if the field is a glide-list. |