Using JSON Array to Create Multiple Records via Scripted REST API

MC_Soria
Kilo Expert

Good day all,

I'm working on an integration using a scripted API to process an inbound JSON message containing an array from which I need to insert separate records into a custom table. I was only able to find the API Reference for the RESTAPIRequestBody for scoped apps, but not for global:

RESTAPIRequestBody

Based on this I'm trying to use the hasNext() & nextEntry() methods to break up the following sample JSON message into separate records in a custom table (u_event_extension):

{
"records": [
{
"u_ref_disc_src_id": 16360,
"u_connection_point": "umi-s1679-t01-m1-au07-d01-ch01-sc01",
"u_call_letters": "XCCT-TV",
"u_dist_short_name": "TEST 1 - XCCT-TV 20.1",
"u_eext_location": "Lebanon, OH",
"u_fd_sid": "2467",
"u_dma_rank": "30",
"u_disc_src_id": "16360",
"u_signal_status": "Up",
"u_signal_timestamp": "2018-05-05 23:43:56",
"u_signal_ticket_number": "EVT0003275963",
"u_signal_last_action": "Opened"
},
{
"u_ref_disc_src_id": 16360,
"u_connection_point": "umi-s1679-t01-m1-au07-d01-ch01-sc02",
"u_call_letters": "WKRP-Radio",
"u_dist_short_name": "TEST 2 - WKRP-TV 20.1",
"u_eext_location": "Cincinnati, OH",
"u_fd_sid": "1234",
"u_dma_rank": "30",
"u_signal_status": "Up",
"u_signal_timestamp": "2018-05-15 23:58:00",
"u_signal_ticket_number": "EVT0003275964",
"u_signal_last_action": "Opened"
}
]
}

Using the following scripted API code (custom table is "u_event_extension"):

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

var requestBody = request.body;
while(requestBody.hasNext()){
var entry = requestBody.nextEntry();

// create new EEXT record
var newExt = new GlideRecord("u_event_extension");
newExt.initialize();

newExt.u_ref_disc_src_id = entry.u_ref_disc_src_id;
newExt.u_connection_point = entry.u_connection_point;
newExt.u_call_letters = entry.u_call_letters;
newExt.u_dist_short_name = entry.u_dist_short_name;
newExt.u_eext_location = entry.u_eext_location;
newExt.u_fd_sid = entry.u_fd_sid;
newExt.u_dma_rank = entry.u_dma_rank;
newExt.u_disc_src_id = entry.u_disc_src_id;
newExt.u_signal_status = entry.u_signal_status;
newExt.u_signal_timestamp = entry.u_signal_timestamp;
newExt.u_signal_ticket_number = entry.u_signal_ticket_number;
newExt.u_signal_last_action = entry.u_signal_last_action;
newExt.u_eext_event_reference = entry.u_eext_event_reference;
newExt.insert();

entry = requestBody.nextEntry();

// send response
response.setBody({
'Number': '' + newExt.u_number,
'Dist Short Name': '' + newExt.u_dist_short_name,
'Sys ID': '' + newExt.sys_id
});
response.setStatus(201);

}

})(request, response);

Right now, when I send the above JSON message to the scripted API URI - it only creates a single record and does not pass any data from the array. What I'm trying to make happen is for the message to create 2 separate records in the u_event_extension table.

Can anyone please tell me what I'm doing wrong or if there is a better way to do this?

Thanks very much in advance!

4 REPLIES 4

joeyfrease
Tera Expert

Hi there, any luck with this?  I've got the same situation and can't seem to find a good example.

 

Thank you in advance,

Joey

harshav
Tera Guru

Did you get any solution for this, i ran in to similar situation.

Phuong Nguyen
Kilo Guru

Hi All,

Looking at the JSON you posted, the first thing that might need to be changed is this "request.body". You want to use request.body.data to get the object containing the information you needed. From there, to get the list of records, you need to do object.results, then you can loop through them like a normal array, something like this to get you started:

var requestBody = request.body.data;
var records = requestBody.records;

for(var i=0; i<records.length; i++){
	//Your logic here, insert new record, etc...
}

Regards,

Phuong

If you find my suggestions helpful, please mark it as correct or helpful to help out others as well 🙂 

Hi Phuong Nguyen,

Need your help on updating the below logic

the below is my REST API Content


"ApplicantId":"abcdef",
"Identity": "${user_id}",
"SystemName":"${name}",
"Reason": "${description}",
"AuthorizationWorkflowType": "ABCD",
"ItRoles":[
{
"RoleName":"${rolename}"
},
{
"RoleName":"${rolename1}"
},
{
"RoleName":"${rolename2}"
},
{
"RoleName":"${rolename3}"
},
{
"RoleName":"${rolename4}"
}
]
}

 

I have to pass multiples values in an array (IT ROLES) and this is the dynamic data.

As per my knowledge we need to use for loop in passing the dynamic data. please provide me with the syntax and logic 

 

Kinldy Help.!!

 

Many Thanks,

Ashwin Umapathi