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,

yes you would require to follow those steps

1) create custom table with those 6 fields; 1 field set to display=true

2) create list type of field on Outage table

3) update script as shared in previous post

try running the script and check whether the outage record has those 4 values in glide list

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,

I have created custom services table and run code that you have provided but i am getting output as : 

find_real_file.png

Could you please help me with this.

 

Regards,

Nivedita

Hi Nivedita,

Can you share complete code here?

Did the records get created in Services table and also 1 record in Outage with those 4 values populated in Service field?

Also can you try to update code as below?

catch(ex) {
gs.info('Exception is: ' + ex);
}

Regards

Ankur

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

Hello Ankur,

Records didn't created in services table.

Can you suggest me here that i am correct or not?

I have created services tab in outage table, that will be remain same right?

I have created services table but records didn't created in services table.

I have update the 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;

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) {
gs.info('Exception is: ' + ex);
//var message = ex.message;

}

 

I am getting below output : 

find_real_file.png

Could you please suggest me for this.

 

 

Regards,

Nivedita

 

Hi Nivedita,

so your script is running fine; ensure following

1) services_table is the correct table name?

2) 6 fields are correct

3) you have correct list type of field on outage table which refers the Services table i.e. services_field

can you try to open the services table and check any records created or not

Since now you are not holding Services information in those tabs; remove those tab and add new field on the form

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