- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2019 12:24 PM
Hello,
How to extract data from response body and set that value in table.
ex - I am calling one API and with that I am getting some records and that are stored in response body. Now I want to extract all the values from response body and set that value in one table.
I am trying to achieve this by schedule job.
Can anyone please help me that how I can extract that values from response body and set that value in our tables.
Regards,
Nivedita
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-07-2019 04:38 AM
Hi Nivedita,
the script shared earlier should work; ensure you give correct field names for the 6 fields
try {
var r = new sn_ws.RESTMessageV2('Get Data', 'Default GET');
//override authentication profile
authentication_type ='basic';
r.setAuthenticationProfile(authentication_type, 'efea940d1b408010acf5fc88cc4bcbf7');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.print(responseBody);
gs.log("response body is" + responseBody);
var obj = JSON.parse(responseBody);
for(var i = 0; i<obj.plannedEvents.length; i++) {
var gr = new GlideRecord('u_outage');
gr.initialize();
gr.u_rollbackplan = obj.plannedEvents[i].rollbackPlan;
gr.u_plannedeventstartdate = obj.plannedEvents[i].plannedEventStartDate;
gr.u_closurecode = obj.plannedEvents[i].closureCode;
gr.u_description = obj.plannedEvents[i].description;
gr.u_plannedeventenddate = obj.plannedEvents[i].plannedEventEndDate;
gr.u_ticketid = obj.plannedEvents[i].ticketId;
gr.u_correlationid = obj.plannedEvents[i].correlationId;
gr.u_impact = obj.plannedEvents[i].impact;
gr.u_executionowner = obj.plannedEvents[i].executionOwner;
gr.u_maintenancetype = obj.plannedEvents[i].maintenanceType;
gr.u_sitelocation= obj.plannedEvents[i].maintenanceType;
gr.u_expecteddowntime= obj.plannedEvents[i].expectedDowntime;
gr.u_activitystatus= obj.plannedEvents[i].activityStatus;
var servicesArray = [];
for(var j = 0; j<obj.plannedEvents[i].services.length; j++) {
var gr = new GlideRecord('services_table');
gr.initialize();
gr.u_serviceidentifier= obj.plannedEvents[i].services[j].serviceIdentifier;
gr.u_protectionstatus= obj.plannedEvents[i].services[j].protectionStatus;
gr.u_servicetype= obj.plannedEvents[i].services[j].serviceType;
gr.u_servicealias= obj.plannedEvents[i].services[j].serviceAlias;
gr.u_customerlename= obj.plannedEvents[i].services[j].customerLeName;
gr.u_cuid= obj.plannedEvents[i].services[j].cuid;
var sysId = gr.insert();
servicesArray.push(sysId.toString());
}
gr.u_services = servicesArray.toString();
gr.insert();
}
gs.log("table data is" + responseBody);
}
catch(ex) {
gs.info('Exception is: ' + ex);
}
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2019 11:30 AM
Hello Ankur,
I was also doing the same but when I am running this code it is showing me error like "not authorized"
Below is my code :
try {
var r = new sn_ws.RESTMessageV2('Get Data', 'Default GET');
//override authentication profile
authentication_type ='basic';
r.setAuthenticationProfile(authentication_type,'efea940d1b408010acf5fc88cc4bcbf7');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.print(responseBody);
var obj = JSON.parse(responseBody);
for(var i = 0; i<obj.plannedEvents.length;i++) {
gs.print("##planned values##"+obj.plannedEvents[i].closureCode);
gs.print("##planned values##"+obj.plannedEvents[i].rollbackPlan);
gs.print("##planned values##"+obj.plannedEvents[i].plannedEventStartDate);
gs.print("##planned values##"+obj.plannedEvents[i].siteLocation);
gs.print("##planned values##"+obj.plannedEvents[i].ticketId);
gs.print("##planned values##"+obj.plannedEvents[i].description);
gs.print("##planned values##"+obj.plannedEvents[i].maintenanceType);
gs.print("##planned values##"+obj.plannedEvents[i].impact);
gs.print("##planned values##"+obj.plannedEvents[i].correlationId);
gs.print("##planned values##"+obj.plannedEvents[i].executionOwner);
gs.print("##planned values##"+obj.plannedEvents[i].plannedEventEndDate);
for(var j = 0;j<obj.plannedEvents[i].services.length;j++) {
gs.print("##Service values###"+obj.plannedEvents[i].services[j].serviceIdentifier);
gs.print("##Service values###"+obj.plannedEvents[i].services[j].protectionStatus);
gs.print("##Service values###"+obj.plannedEvents[i].services[j].serviceType);
gs.print("##Service values###"+obj.plannedEvents[i].services[j].serviceAlias);
gs.print("##Service values###"+obj.plannedEvents[i].services[j].customerLeName);
gs.print("##Service values###"+obj.plannedEvents[i].services[j].cuid);
}
}
gs.print("##planned Description##"+obj.plannedEvents.length);
gs.print("##Service###"+obj.plannedEvents[0].services.length);
gs.print("##Hello##"+obj.message);
//var parser = new global.JSON();
//var parsed = parser.decode(responseBody);
//gs.log("Parsed Data1:"+parsed.result.siteLocation);
gs.log('responsebody is' + responseBody);
}
catch(ex) {
var message = ex.message;
}
Can you please suggest me where i am doing wrong.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2019 11:37 AM
Hello Ankur,
Thank you so much for your help.
I tried to get all the values but it is showing as "not authorized" don't know why.
Can you please help me on how i can get all the values from JSON and import that values in my custom table.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2019 12:31 PM
Hello Ankur,
Now my code is working as expected. I am getting all the values from JSON.
I need to import these values to my table.
Could you please help me with this.
Regards,
Nivedita
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-02-2019 08:42 PM
Hi Nivedita,
once you get the values now you need to know to which table you need to insert and what values you want to insert i.e. map value to particular field of table
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2019 04:03 AM
Hello Ankur,
Thank you so much for your reply.
I need to insert all the data into my custom table.
I have written below code but when i am running this background script it is showing some output. When i see my custom table then data is not filled in table.
Below is my code :
try {
var r = new sn_ws.RESTMessageV2('Get Data', 'Default GET');
//override authentication profile
authentication_type ='basic';
r.setAuthenticationProfile(authentication_type, 'efea940d1b408010acf5fc88cc4bcbf7');
var response = r.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.print(responseBody);
gs.log("response body is" + responseBody);
var obj = JSON.parse(responseBody);
for(var i = 0; i<obj.plannedEvents.length; i++) {
var gr = new GlideRecord('u_outage');
gr.initialize();
gr.u_rollbackplan = obj.plannedEvents[i].rollbackPlan;
gr.u_plannedeventstartdate = obj.plannedEvents[i].plannedEventStartDate;
gr.u_closurecode = obj.plannedEvents[i].closureCode;
gr.u_description = obj.plannedEvents[i].description;
gr.u_plannedeventenddate = obj.plannedEvents[i].plannedEventEndDate;
gr.u_ticketid = obj.plannedEvents[i].ticketId;
gr.u_correlationid = obj.plannedEvents[i].correlationId;
gr.u_impact = obj.plannedEvents[i].impact;
gr.u_executionowner = obj.plannedEvents[i].executionOwner;
gr.u_maintenancetype = obj.plannedEvents[i].maintenanceType;
gr.u_sitelocation= obj.plannedEvents[i].maintenanceType;
gr.u_expecteddowntime= obj.plannedEvents[i].expectedDowntime;
gr.u_activitystatus= obj.plannedEvents[i].activityStatus;
for(var j = 0; j<obj.plannedEvents[i].services.length; j++) {
gr.u_serviceidentifier= obj.plannedEvents[i].services[j].serviceIdentifier;
gr.u_protectionstatus= obj.plannedEvents[i].services[j].protectionStatus;
gr.u_servicetype= obj.plannedEvents[i].services[j].serviceType;
gr.u_servicealias= obj.plannedEvents[i].services[j].serviceAlias;
gr.u_customerlename= obj.plannedEvents[i].services[j].customerLeName;
gr.u_cuid= obj.plannedEvents[i].services[j].cuid;
}
gr.insert();
gs.log("table data is" + responseBody);
}
catch(ex) {
var message = ex.message;
}
Below is my output :
Could you please tell me where I am doing wrong.
Regards,
Nivedita