Set value in reference field glide ajax

Priya75
Tera Contributor

Hi All,

 

I have created a glideajax to send data from client to script include, so the field is on the form: - ABC.

So I am sending sys_id in string form and sending this data to client side, the data is coming but how to set this list in reference field.

 

I am using g_form.setValue("field_name" , answer);

 

7 REPLIES 7

Astik Thombare
Tera Sage

Hi @Priya75 ,

 

You can use alert to check what values you are getting from server side like 

 

 

alert(answer);

 

 

 

if you are getting values in JSON Format then you need to parse it and need to extract exact value from the answer and in case of XML . you can directly set value by using below script 

 

 

 

g_form.setValue('field_name',answer);

 

 

 

Refer -

 

XML - https://www.servicenow.com/community/developer-articles/set-values-on-client-scripts-getreference-gl...

 

JSON -https://www.servicenow.com/community/developer-forum/populate-field-value-based-on-another-field-using-glide...

 

If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

                         By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

 

Astik

 

i am sending the list of records which are sys_id of records and i am sending it as a string comma separated.

Community Alums
Not applicable

Hi @Priya75 ,

Try using this client script,

function setReferenceFieldValues(sysIds) {
var sysIdArray = sysIds.split(','); // Split the comma-separated string into an array of sys_ids

g_form.setValue('reference_field_name', sysIdArray); // Set the value of the reference field using the array of sys_ids
}

function getReferenceFieldValues() {
var ga = new GlideAjax('MyScriptInclude'); // Create a GlideAjax object
ga.addParam('sysparm_name', 'getSysIds');
ga.getXMLAnswer(function(response) {
if (response) {
setReferenceFieldValues(response);
} else {
alert('Error: No response received from server.');
}
});
}

getReferenceFieldValues(); // Call the function to retrieve sys_ids and set the reference field values


Could you please share screenshot of script . to understand better ?

 

Thanks,
Astik