Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Scripted Rest API parse JSON

ASHLE CRAIG
Giga Contributor

I'm Implementing a change registration endpoint so that changes can be created via an API. So Im Building out a Scripted REST API here and a little stuck. Im scripting to populate these fields (Service, Assignment group, description, short description, planned start date , planned end date). 

Am I off to the right start? How do I scripted for planned start date/ planned end date for the format to be like (2022-07-26T16:07:05Z-3:00)

Heres what I have so far:

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

// implement resource here
var requestBody=request.body.data;
var parsedData=JSON.parse(requestBody);
 
var desc=parsedData.description;
var shortDesc=parsedData.short_description;
var service=parsedData.business_service;
var assignGrp=parsedData.assignment_group;
var plannedStart=parsedData.start_date;
var plannedEnd=parsedData.end_date;


var gr=newGlideRecord("change_request");
gr.initialize();
gr.description = desc;
gr.short_description = shortDesc;
gr.business_service = service;
gr.assignment_group = assignGrp;
gr.start_date = plannedStart;
gr.end_date = plannedEnd;

gr.insert();

varbody= {};
body.number=gr.number;

response.setBody(body);

})(request, response);
1 ACCEPTED SOLUTION

Sathiskumar_D
Giga Sage

Hi Ashle,

 

for planned start date, can you try like this.

 

var startDate=parsedData.start_date;

var plannedStartDate=startDate.split('T')[0];

 

 

View solution in original post

5 REPLIES 5

Sathiskumar_D
Giga Sage

Hi Ashle,

 

for planned start date, can you try like this.

 

var startDate=parsedData.start_date;

var plannedStartDate=startDate.split('T')[0];

 

 

Thank you! 

Hi Sathish,

Should I have both queries in the script you suggested ? Or just

var plannedStartDate=startDate.split('T')[0];

ASHLE CRAIG
Giga Contributor

As suggested ?

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

// implement resource here
var requestBody=request.body.data;
var parsedData=JSON.parse(requestBody);
var desc=parsedData.description;
var shortDesc=parsedData.short_description;
var service=parsedData.business_service;
var assignGrp=parsedData.assignment_group;
var startDate=parsedData.start_date;
var plannedStartDate=startDate.split('T')[0];
var endDate=parsedData.end_date;
var plannedEndDate=endDate.split('T')[0];



vargr=newGlideRecord("change_request");
gr.initialize();
gr.description=desc;
gr.short_description=shortDesc;
gr.business_service=service;
gr.assignment_group=assignGrp;
gr.start_date=startDate;
gr.end_date=endDate;

gr.insert();

varbody= {};
body.number=gr.number;

response.setBody(body);

})(request, response);