Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Trouble pulling in JSON data from webhook

Ken Berger
Giga Guru

Hi folks,

 

I have the following code on a Scripted REST Resource, to pull in the JSON data being sent via a webhook:

 

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

    gs.info(request.body.dataString);
	var grIncident = new GlideRecord('incident');
	grIncident.initialize();
	grIncident.caller_id = 'TEST Caller';
	grIncident.assignment_group = <sysIdOfAssignmentGroup>;
	grIncident.category = 'Software';
	grIncident.subcategory = <mySoftware>;
	grIncident.short_description = request.body.summary;
	grIncident.description = request.body.dataString;
	grIncident.insert();
	
})(request, response);

 

 

Here is the JSON that is coming over from the webhook (verified in the system logs):

 

{
	"summary":"Started replicating logs for Microsoft SQL Server Database 'fakeDB' on 'MY-FAKESERVER\\\\MYARCHIVE' from 'MY-CLUSTER' to 'CLUSTER-NAME'",
	"source":"some-system-id | some-other-system-id",
	"severity":"info",
	"timestamp":"2024-02-14T14:28:51.821Z",
	"class":"Replication",
	"custom_details":{"seriesId":"4c4de3e2-e512-4039-af47-7a29d88363c5",
	"id":"59575c16-1c9f-4b3a-93d6-d904653892bc",
	"type":"Event",
	"objectId":"fdf2458a-039c-5d34-bc03-667da1800a3e",
	"objectName":"fakeDB",
	"objectType":"Mssql",
	"status":"Running",
	"clusterId":"some-other-randon-string-of-characters",
	"clusterName":"MY-CLUSTER",
	"eventName":"Mssql.LogReplicationStarted",
	"errorId":"",
	"errorCode":"",
	"errorRemedy":"",
	"errorReason":"",
	"auditUserName":"",
	"auditUserId":"",
	"location":"MY-LOCATION\\MYARCHIVE",
	"url":""}
}

 

It's probably something simple I am overlooking but I can't get the description and short description fields to populate.  Any ideas on what I am doing wrong, or what I may have missed?

 

Thanks,

Ken

 

 

1 ACCEPTED SOLUTION

kps sumanth
Mega Guru

Hello @Ken Berger ,

If you are setting all the required fields in the above script then I don't think so you need query parameters.

View solution in original post

3 REPLIES 3

kps sumanth
Mega Guru

Hello @Ken Berger ,

 

Can you try parsing the request body as below,

 

var bodyString = request.body.dataString + '';
    var requestObj = JSON.parse(bodyString + '');
Then try to set short description and description.
 
Please mark this as Accepted and Helpful if this worked for you.

Ken Berger
Giga Guru

Hi kps2,

 

That helped a bunch!  Thank you.

 

I had to use JSON.stringify() on the description field but that worked a treat.  Here is the code if it could help someone else:

 

 

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

	var bodyString = request.body.dataString + '';
    var requestObj = JSON.parse(bodyString + '');
	
    gs.info(request.body.dataString);
	var grIncident = new GlideRecord('incident');
	grIncident.initialize();
	grIncident.caller_id = 'TEST Caller';
	grIncident.assignment_group = <sysIdOfAssignmentGroup>;
	grIncident.category = 'Software';
	grIncident.subcategory = <mySoftware>;
	grIncident.short_description = requestObj.summary;
	grIncident.description = JSON.stringify(requestObj);
	grIncident.insert();
	
})(request, response);

 

 

One more quick question, if you will indulge me...  Doing it this way, is it necessary to add the query parameters and associations in my API and resource?  I added them in DEV but when I move to PROD, I would not need them, right?

 

Thanks,

Ken

kps sumanth
Mega Guru

Hello @Ken Berger ,

If you are setting all the required fields in the above script then I don't think so you need query parameters.