Set reference field from ajax call in catalog client script

Jalen
Tera Contributor

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?

 

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

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.

The SN Nerd
Giga Sage
Giga Sage

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

Shillu
Kilo Guru

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.

 

Note: The method setValue() can cause a stack overflow when used in an OnChange client script. This is because every time the value is set, it will register as a change, which may re-trigger the OnChange client script. To prevent this, perform a check that will validate that the new value will be different from the old value. For example, before performing setValue(shortDesc, newValue.toUpperCase());, validate that the short description is not already uppercase. This will prevent the client script from applying the toUpperCase() more than once.

 

Parameter(s):
NameTypeDescription
fieldNameStringName of the field.
valueStringSystem ID for the reference value in the database. Can be an array of system IDs if the field is a glide-list.
displayValueStringDisplay name for the referenced value in the database. Can be an array of display names if the field is a glide-list.