REST API to send records details

omsa1
Kilo Guru

Hi All,

I need help with REST API . i'm trying to establish communication with monitoring tool. 

when state of change request changes to "implement", i need to send that record information via REST to monitoring tool.

i need to send variables in this format. 

<Task_type>, <CHG Ticket number>,<Start time in UTC>,<End time in UTC>,<CHG short description>,<CHG requestor (firstname.lastname)>,<CI list separated with a delimiter> 

i tried with REST API Explorer using POST and getting result as below. But what i actually need is

1.The <task_type> i need to add values manually like "ADD" or "DELETE" 

2. Add CI list separated with a delimiter <CI list separated with a delimiter>  , we have related list called "affected CIs" for change_request, i need to get the values from this related list to populate in the response , separate with delimiter. 

response example : 'ADD,CHG0123456,20190607152055,20190607162055,system down,nisha.rahul,host1;host2;host3'

 

find_real_file.png

 

 

 

6 REPLIES 6

Harsh Vardhan
Giga Patron

why don't you create here scripted rest API here and there you can do the glide record to get the related list records details. 

 

SAmple code:

 

Create one script include here and write the glide-record to get the form details as well as related list details. 

 

var getInc = Class.create();
getInc.prototype = {
    initialize: function() {
    },
	
	getDetails: function(number){
		
		var arr=[];
		var gr = new GlideRecord('incident');
		gr.addQuery('number',number);
		gr.query();
		if(gr.next()){
			
			var rl = new GlideRecord('task_sla');
			rl.addQuery('task.number',number);
			rl.query();
			if(rl.next()){
				
				
						
			arr.push({
				
				'Number': gr.number+'',
				'Assigned To': gr.assigned_to.getDisplayValue()+'',
				'State': gr.state.getDisplayValue()+'',
				'Stage': rl.stage.getDisplayValue()
				
			});
			return arr;
		}
		}
		
	},

    type: 'getInc'
};

 

Scripted rest API:

 

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

    // implement resource here
	return new getInc().getDetails(request.pathParams.number);

})(request, response);

 

Note: I tested here to get incident records details as well as task sla related list stage details. 

 

https://docs.servicenow.com/bundle/london-application-development/page/integrate/custom-web-services...

 

 

Hi Harshvardhan,

Thanks for the suggestion.

 

I tried the steps above and i'm getting empty response. my script and settings as below. 

find_real_file.png

 Script include :-

var MonolithAlertSuppress = Class.create();
MonolithAlertSuppress.prototype = {
initialize: function() {
},
getDetails: function(number){
var ci=[];
var arr=[];
var gr = new GlideRecord('change_request');
gr.addQuery('number',number);
gr.query();
if(gr.next()){

var rl = new GlideRecord('task_ci');
rl.addQuery('task',number);
rl.query();
while (rl.next()){
var ciList = rl.getDisplayValue("ci_item");
ci.push(ciList);

arr.push({
'Task Type':"ADD",
'Number': gr.number+'',
'Start time in UTC': gr.start_date.getDisplayValue()+'',
'End time in UTC': gr.end_date.getDisplayValue()+'',
'Short Description': gr.short_description.getDisplayValue()+'',
'Requested by': gr.requested_by.getDisplayValue()+'',
'CI': ciList+'',

});
return arr;
}
}

},

type: 'MonolithAlertSuppress'
};

 

scritped REST api settings

find_real_file.png

 

find_real_file.png

are you using POST method ? because the example which i have updated here that is related to GET. 

 

if you wanna do it for POST then you have the pass something like below 

 

var requestBody = request.body.data;
response.setContentType('application/json');

var result= new getInc().getDetails(request.pathParams.number);

response.setBody({'result': result});

 

 

And in relative path field on your method define /{number} to test this. 

Harsh Vardhan
Giga Patron

any update on this thread. 

if i answered your query, kindly mark the answer correct and close this thread.