How do I process the HTTP response to a REST API call?

Wayne Richmond
Tera Guru

Hi all. I am learning as I go on this one and have hit a wall here.

Scenario: When an incident is assigned to a third-party vendor, details are sent via REST to their ticketing system to log a ticket. We should receive a reference number back which updates our incident.

I have created an Outbound REST message to the third-party's endpoint. I have created a Business Rule that triggers the outbound message and generates an HTTP Request (POST). I get a successful response back that contains a reference number from the third-party. I need to take that reference number and update the incident that generated the message. How do I do this? 

I am able to parse the response to a log entry but am not sure how I can use it beyond that. Below is the Business Rule that generates the REST call and creates the logs for me. You can see how I attempted to update the ticket from this BR.

When to run:
When: Before
Insert/Update

Script:

(function executeRule(current, previous /*null when async*/ ) {
    gs.info('BY Integration - Business Rule ran');
    // pass our incident details to the http Method (note, the first parameters are variables names in the http Method, NOT ServiceNow field names)
    
	var desc = '';
	if (current.description == '') {
		desc = current.short_description;
	} else desc = current.description;
	
	var request = new sn_ws.RESTMessageV2('VendorName Incident Integration', 'Create Incident'); // REST Message Name, Method Name
    request.setStringParameterNoEscape('reference_number', current.number);
    request.setStringParameterNoEscape('priority', current.priority);
    request.setStringParameterNoEscape('subject', current.short_description);
    request.setStringParameterNoEscape('description', desc);
    request.setStringParameterNoEscape('first_comment', current.work_notes);
    request.setStringParameterNoEscape('assetid', current.u_vendor_category);

    var response = request.execute();
    var requestBody = request.getRequestBody();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
    var errorMsg = response.getErrorMessage();
    gs.info('BY Integration - Request Body:' + requestBody);
    gs.info('BY Integration - Response Body:' + responseBody);

    var parsedJSON = JSON.parse(responseBody);

    var BYIncidentNumber = parsedJSON['CaseNumber'];
	var BYIncidentSysID = parsedJSON['CaseId'];

    gs.info('BY Integration - BY Incident Number: ' + BYIncidentNumber + '\nBY Incident sysID: ' + BYIncidentSysID);
	
	//current.setValue('u_vendor_reference_number', BYIncidentNumber);
	//current.setValue('u_vendor_sys_id', BYIncidentSysID);

})(current, previous);

Here is an example of the respone from my logs:

BY Integration - Response Body:{
  "CaseNumber": "03491683",
  "CaseId": "5008J000000iCkPQAU",
  "Status": "New",
  "ResponsibleParty": "Support",
  "Resolution__c": null
}
1 ACCEPTED SOLUTION

Wayne Richmond
Tera Guru

I got it working. I switched When to asnyc and added current.update() to my BR:

(function executeRule(current, previous /*null when async*/ ) {
    gs.info('BY Integration - Business Rule ran');
    // pass our incident details to the http Method (note, the first parameters are variables names in the http Method, NOT ServiceNow field names)

    var desc = '';
    if (current.description == '') {
        desc = current.short_description;
    } else desc = current.description;

    var request = new sn_ws.RESTMessageV2('VendorName Incident Integration', 'Create Incident'); // REST Message Name, Method Name
    request.setStringParameterNoEscape('reference_number', current.number);
    request.setStringParameterNoEscape('priority', current.priority);
    request.setStringParameterNoEscape('subject', current.short_description);
    request.setStringParameterNoEscape('description', desc);
    request.setStringParameterNoEscape('first_comment', current.work_notes);
    request.setStringParameterNoEscape('assetid', current.u_vendor_category);

    var response = request.execute();
    var requestBody = request.getRequestBody();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
    var errorMsg = response.getErrorMessage();
    gs.info('BY Integration - Request Body:' + requestBody);
    gs.info('BY Integration - Response Body:' + responseBody);

    var parsedJSON = JSON.parse(responseBody);

    var BYIncidentNumber = parsedJSON['CaseNumber'];
    var BYIncidentSysID = parsedJSON['CaseId'];

    gs.info('BY Integration - BY Incident Number: ' + BYIncidentNumber + '\nBY Incident sysID: ' + BYIncidentSysID);

    // Update our incident with the BY reference numbers from the HTTP response
    current.setValue('u_vendor_reference_number', BYIncidentNumber);
    current.setValue('u_vendor_sys_id', BYIncidentSysID);
    current.update();

})(current, previous);

View solution in original post

4 REPLIES 4

Mohit Kaushik
Mega Sage
Mega Sage

Hi Wayne,

you can get the value by using below code:

parsedJSON.CaseNumber;
parsedJSON.CaseId;

 

You can refer this link as well:

Parsing Data from the Response | ServiceNow Developer

 

Please mark this as correct and helpful if it resolved the query or lead you in right direction.

Thanks,
Mohit Kaushik
Community Rising Star 2022

 

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Hi. Thanks. I have no problem getting the value, I just don't know how to update my incident with it.

Wayne Richmond
Tera Guru

I got it working. I switched When to asnyc and added current.update() to my BR:

(function executeRule(current, previous /*null when async*/ ) {
    gs.info('BY Integration - Business Rule ran');
    // pass our incident details to the http Method (note, the first parameters are variables names in the http Method, NOT ServiceNow field names)

    var desc = '';
    if (current.description == '') {
        desc = current.short_description;
    } else desc = current.description;

    var request = new sn_ws.RESTMessageV2('VendorName Incident Integration', 'Create Incident'); // REST Message Name, Method Name
    request.setStringParameterNoEscape('reference_number', current.number);
    request.setStringParameterNoEscape('priority', current.priority);
    request.setStringParameterNoEscape('subject', current.short_description);
    request.setStringParameterNoEscape('description', desc);
    request.setStringParameterNoEscape('first_comment', current.work_notes);
    request.setStringParameterNoEscape('assetid', current.u_vendor_category);

    var response = request.execute();
    var requestBody = request.getRequestBody();
    var responseBody = response.getBody();
    var httpStatus = response.getStatusCode();
    var errorMsg = response.getErrorMessage();
    gs.info('BY Integration - Request Body:' + requestBody);
    gs.info('BY Integration - Response Body:' + responseBody);

    var parsedJSON = JSON.parse(responseBody);

    var BYIncidentNumber = parsedJSON['CaseNumber'];
    var BYIncidentSysID = parsedJSON['CaseId'];

    gs.info('BY Integration - BY Incident Number: ' + BYIncidentNumber + '\nBY Incident sysID: ' + BYIncidentSysID);

    // Update our incident with the BY reference numbers from the HTTP response
    current.setValue('u_vendor_reference_number', BYIncidentNumber);
    current.setValue('u_vendor_sys_id', BYIncidentSysID);
    current.update();

})(current, previous);

Glad its working for you. You can mark this question as correct answer so that it can be moved from unanswered queue and others can get help from this.

 

Thanks,
Mohit Kaushik
Community Rising Star 2022

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)