How to send Glide Records after quering database from Script Include in JSON format

G Sai Vamshi
Mega Guru

I have a requirement to Glide a table with specific conditions and result of that query ie records of that table should be returned in JSON Format.

7 REPLIES 7

Emmanuel Jay Mu
Tera Contributor

You can check out this page (Convert gliderecord to a JSON object), this might answer your question.

Harish KM
Kilo Patron
Kilo Patron

Hi @G Sai Vamshi here is the sample code to get User details

getUser: function()
{
var userInform ={};
var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", gs.getUserID());
gr.query();
if (gr.next()){
userInform.name = gr.getValue("name");
userInform.company = gr.getValue("company");
}

var result= JSON.stringify(userInform); // JSON object

return result;

},

Regards
Harish

@Harish KM  Thanks for your quick response. My requirement is to send the whole user record and user records will be multiple records not a single record. In your code, you are taking single record and only two values. Really appreciate if you throw some insights on my requirement.

Hi @G Sai Vamshi to push multiple values you would need arrays and then covert to JSON as below

 

getUser: function()
{

var userData = [];

var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", gs.getUserID());
gr.query();
while (gr.next()){

var userInform = {};
userInform.name = gr.getValue("name");
userInform.company = gr.getValue("company");
userData.push(userInform);
}
// Convert the array of records to a JSON object

var result= JSON.stringify(userData);

return result;

},

 

 

Regards
Harish