Object Output to String Upon GlideAjax

win b
Mega Guru

Hi can you help me on this. i know its kind of correct but i think i missed on some steps on the ff scenario for retrieving the value of serial number in the cmbd_ci table. on my client script i did an onchange script were it gives me the value of the serial number but upon setting the value it display as an object not on string. 
find_real_file.png

in my Script Include(SI) here is what i did.

find_real_file.png

and upon running in my portal this is what i got
on my first alert find_real_file.png

on my second alert
find_real_file.png
what i got on the setvalue is obj which supposed to be the serial number 
find_real_file.png

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

Hello @win b ,

No need to JSON just you can do like below in script include 

Remove the JSON and 7th and 15th line script include 

return existing.serial_number.toString();

 

Client script:

remove 17th line and just set the answer to the variable in set value

g_form.setValue('change_request_no',answer);

 

Hope this helps

Please mark my answer correct if this helps you

 

View solution in original post

13 REPLIES 13

Harshad Wagh
Tera Guru

in your script include you are returning the "existing" object.

Please replace it with following line (Line no 20)

 

return JSON.stringify(existci);

 

@Harshad Wagh  when i tried that it displays only this {} on my value 

Abhijit4
Mega Sage

Update your script past as below,

Client script : 

var ga = new GlideAjax( 'SI_for_Generic_CMDB');
ga.addParam('sysparm_name', 'getSerialNo');
ga.addParam('sysparm_sysID', newValue);
ga.getXMLAnswer(Callback);
function Callback(response) {
g_form.setValue('change_request_no', response);
}

Script Include :

getSerialNo: function() {
var serialNumber="";
var sysID = this.getParameter('sysparm_sySID'); 
var existing = new GlideRecord('cmdb_ci'); 
if (existing.get(sysID)) {
serialNumber= existing.serial_number;
}
return serialNumber;

Let me know if you have any further queries.

Please mark this as Correct or Helpful if it helps.

Regards,
Abhijit
Community Rising Star 2022

By marking my response as correct or helpful, you contribute to helping future readers with similar issues.
Regards,
Abhijit
ServiceNow MVP

i tried your code too but it displays on my end as blank