how to extract data from response body.

niveditakumari
Mega Sage

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 

1 ACCEPTED SOLUTION

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

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

55 REPLIES 55

Hi Nivedita,

the 4 services tab in the form are having different 6 different fields in each i.e.

1st tab - 6 fields

2nd tab -  another 6 fields

etc

Or the same set of 6 fields are shown in each tab?

Regards

Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur,

In all tabs it is having same set of fields are there but value will be different because services is nested and under services there are four records for services.

"services":[{"serviceIdentifier":"TEST1-CN-862128916674-G-MPLS_duplicate","protectionStatus":"Protected","serviceType":"GVPN - MPLS","serviceAlias":"","customerLeName":"TATA Communications Test1","cuid":"1"},

{"serviceIdentifier":"TEST1-GB-441926640000-IP-DIA-40","protectionStatus":"Protecting","serviceType":"IP - DIA","serviceAlias":"","customerLeName":"TATA Communications Test1","cuid":"1"},

{"serviceIdentifier":"TEST1-GB-040237-I-TPAAS-02","protectionStatus":"Unprotected","serviceType":"Infrastructure - TPaaS","serviceAlias":"","customerLeName":"TATA Communications Test1","cuid":"1"},

{"serviceIdentifier":"TEST1-GB-040237-G-MPLS-01","protectionStatus":"Protection failure","serviceType":"GVPN - MPLS","serviceAlias":"","customerLeName":"TATA Communications Test1","cuid":"1"}]

 

Regards,

Nivedita

Hello Ankur,

I want to set values under all services tab but it is not setting value correctly.

Any suggestions that how I can set values under services tab.

 

Regards,

Nivedita

Hi Nivedita,

If the same set of 6 fields are present in those 4 tables then only 1 set of services value from json will be saved;

the remaining will get overridden. if you want to store all then I would recommend to have either of these approaches:

1) have 24 fields; i.e. 6 each for each tab which will be unique

2) create custom services table

a) have a list type of field on your Outage table which refers this Services table

b) services table will have only 6 fields

c) populate the list type of field on Outage with all the services records i.e. sys_id

I would recommend 2nd approach since it is easier to manage instead of creating 24 fields

updated code as per 2nd approach

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.services_field = servicesArray.toString();
gr.insert();

}
gs.log("table data is" + responseBody);
}


catch(ex) {
var message = ex.message;

}

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hello Ankur,

Thank you so much for your reply.

Could you please provide me the steps to achieve 2nd approach.

Should I do below steps :

1. create custom services table with six fields.

2. create new field with list type on outage table and that will refers to services table that i created.

What could be the next steps i need to do.

Could you please correct me if i am wrong.

 

Regards,

Nivedita