- 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-07-2019 02:13 AM
Hi Nivedita,
Can yo share the script?
Also mention the table name correctly, the list field correctly and it should work
Regards
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-07-2019 04:27 AM
Hello Ankur,
Thank you so much for your reply.
1. I have created one outage(u_outage) table.
(a) I have created one field on outage table with list type and in reference i have given as services table.
2. I have created one services(u_services) table with 6 fields
serviceidentifier
protectionStatus
serviceType
serviceAlias
customerleName
cuid
Please find the below screenshot :
Could you please provide code for this then i can try with that code.
Regards,
Nivedita
- 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-01-2019 02:52 AM
Hi Nivedita,
Please go though below link
If my answer helped you in any way, please then mark it correct and helpful.
Kind regards,
Manas
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-01-2019 03:28 AM
Hello Nivedita,
Check out the below codeL
var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();
var obj = JSON.parse(responseBody); //define JSON parsing for the response JSON file to decode
var targetIncidentID = obj.result.sys_id;
current.u_target_record_id = targetIncidentID;
current.update();
gs.log("HTTPCODE:"+httpStatus+"BODY"+response.getBody());
gs.log("AJG targetIncidentID: "+targetIncidentID);
Please mark as Correct Answer and Helpful, if applicable.
Thanks!
Abhishek Gardade
Hexaware Technologies
Abhishek Gardade