The CreatorCon Call for Content is officially open! Get started here.

inserting org.mozilla.javascript.NativeArray data instead of a json string in service now table

pavani19
Kilo Expert

i want to insert the rest api respopnse in service now table. in our company we are not allowed to create new tables, i need to insert the response in a table as json string.

please find the code.

var RL_LinkDetails = Class.create();
RL_LinkDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {


callRLapi : function(){
var r = new sn_ws.RESTMessageV2('RL_rldbApi', 'Get_LinkDetails');

//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthentication(authentication type, profile name);

midserver = gs.getProperty('mid.server.rba_default');
r.setMIDServer(midserver);

var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

var res= JSON.parse(responseBody);

//for(var i=0; i<res.length; i++)  just want to try for 5 rows of data

for(var i=0; i<5; i++)
{
var ld = new GlideRecord('u_request_choices_lookup');
ld.u_label='rl';
ld.u_secondary_label='linkdetails';
ld.u_attributes=res[i].userKey;
ld.u_value= [

{"url":res[i].url},
{"shortDesc":res[i].shortDesc}
];

ld.insert();


}
},


type: 'RL_LinkDetails'
});

 

i am seeing the value column of the table showing data as org.mozilla.javascript.NativeArray.

please find the attached screen shot of the table.

 

find_real_file.png

1 ACCEPTED SOLUTION

var ex_value = [

{"url":res[i].url},
{"shortDesc":res[i].shortDesc}
];

ld.u_value=JSON.stringify(ex_value);
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

4 REPLIES 4

Mateen
Giga Guru

try this

var ex_value = [

{"url":res[i].url},
{"shortDesc":res[i].shortDesc}
];

ld.u_value=ex_value.toString();

 

Basically you have convert the array to string. 

Thanks for responding Mateen.

But i am not seeing the correct data. its inserted  [object Object],[object Object].

find_real_file.png

 

var ex_value = [

{"url":res[i].url},
{"shortDesc":res[i].shortDesc}
];

ld.u_value=JSON.stringify(ex_value);
Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022