-
Notifications
You must be signed in to change notification settings - Fork 12
/
servicenow.js
319 lines (284 loc) · 9.82 KB
/
servicenow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
'use strict';
var request = require('request');
var querystring = require('querystring');
/**
* @typedef ClientConfig
* @type {object}
* @property {string} instance - base URL to instance i.e. "https://demo.servicenow.com"
* @property {string} username - login of the user to act on behalf of
* @property {string} password
*/
/**
* Builds a connection to a specific instance
* @class
*
* @example
* var servicenow = require('servicenow');
* var config = {
* instance: "https://demo.servicenow.com",
* username: "admin",
* password: "admin"
* };
* var client = new servicenow.Client(config);
*
* @constructor
* @param {ClientConfig} config - config object
*
*/
function Client(config) {
this.instance = config.instance;
this.config = config;
this.request = request.defaults({
auth: {
user: config.username,
pass: config.password,
sendImmediately: true
},
headers: {
"Content-Type": "application/json"
}
});
}
/**
* This is the signature for all methods in this module that take a callback function as their final parameter.
* @callback requestCallback
* @param {string|object} error - Any error generated, null if no errors
* @param {object} result - parsed object as returned from REST service
*/
/**
* Gets the sys_id for all records matching the `query`.
*
* @param {string} table - the table to query
* @param {string} query - [encoded query string]{@link http://wiki.servicenow.com/index.php?title=Embedded:Encoded_Query_Strings}
* @param {requestCallback} callback - response handler
*
* @example
* client.getKeys("incident","Active=true",function(error,result) {
* if(!error) {
* // result contains an array of sys_ids
* }
* });
*
*/
Client.prototype.getKeys = function(table,query,callback) {
var url = this.instance + "/" + table + ".do?JSONv2=&";
var params = querystring.stringify({
"sysparm_action": "getKeys",
"sysparm_query" : query
});
this.request.get(url + params, function(err,response,body) {
restCallbackHandler(err,response,body,callback);
});
};
/**
* Retrieves the object matching `sysid` from `table`
*
* @param {string} table - the table to query
* @param {string} sysid - sys_id of the object to load
* @param {requestCallback} callback - response handler
* @param {boolean} [displayValue=false] - true if references should be decoded, false returns sys_id. See {@link http://wiki.servicenow.com/index.php?title=JSONv2_Web_Service#Return_Display_Value_for_Reference_Variables Reference Variables}
* @param {boolean} [displayVariables=false] - true if variables array should be returned with the response, see {@link http://wiki.servicenow.com/index.php?title=JSONv2_Web_Service#Return_Display_Variables Display Variables}
*
* @example
* client.getKeys("incident","Active=true",function(error,result) {
* if(!error) {
* // result contains the object
* }
* });
*
*/
Client.prototype.get = function(table,sysid,callback,displayValue,displayVariables) {
var url = this.instance + "/" + table + ".do?JSONv2=&";
var params = querystring.stringify({
"sysparm_action": "get",
"sysparm_sys_id" : sysid,
"displayvalue" : (displayValue ? true : false),
"displayvariables" : (displayVariables ? true : false)
});
this.request.get(url + params, function(err,response,body) {
restCallbackHandler(err,response,body,callback);
});
};
/**
* Gets all records matching the `query`.
*
* @param {string} table - the table to query
* @param {string} query - [encoded query string](http://wiki.servicenow.com/index.php?title=Embedded:Encoded_Query_Strings)
* @param {requestCallback} callback - response handler
* @param {boolean} [displayValue=false] - true if references should be decoded, false returns sys_id. See {@link http://wiki.servicenow.com/index.php?title=JSONv2_Web_Service#Return_Display_Value_for_Reference_Variables Reference Variables}
* @param {boolean} [displayVariables=false] - true if variables array should be returned with the response, see {@link http://wiki.servicenow.com/index.php?title=JSONv2_Web_Service#Return_Display_Variables Display Variables}
*
* @example
* client.getRecords("incident","Active=true",function(error,result) {
* if(!error) {
* // result contains an array of objects matching query
* }
* });
*/
Client.prototype.getRecords = function(table,query,callback,displayValue,displayVariables) {
var url = this.instance + "/" + table + ".do?JSONv2=&";
var params = querystring.stringify({
"sysparm_action": "getRecords",
"sysparm_query": query,
"displayvalue": (displayValue ? true : false),
"displayvariables" : (displayVariables ? true : false)
});
this.request.get(url + params, function(err,response,body) {
restCallbackHandler(err,response,body,callback);
});
};
/**
* Updates records(s) in `table` matching `query` with fields in `object`.
*
* @param {string} table - the table to update
* @param {string} query - [encoded query string](http://wiki.servicenow.com/index.php?title=Embedded:Encoded_Query_Strings) matching the record(s) to update
* @param {object} object - object containing fields to update
* @param {requestCallback} callback - response handler
*
* @example
* var o = {
* "state": "1"
* }
* client.update("incident","number=INC0000001",o,function(error,result) {
* if(!error) {
* // result contains array of full updated objects
* }
* });
*/
Client.prototype.update = function(table,query,object,callback) {
var url = this.instance + "/" + table + ".do?JSONv2=&";
var params = querystring.stringify({
"sysparm_action": "update",
"sysparm_query" : query
});
var opts = {
body: JSON.stringify(object)
};
this.request.post(url + params, opts, function(err,response,body) {
restCallbackHandler(err,response,body,callback);
});
};
/**
* Inserts `object` into `table`. Multiple records can be inserted if `object` contains an array of objects named `records`.
*
* @param {string} table - the table to insert into
* @param {object|array} object - object(s) to insert
* @param {requestCallback} callback - response handler
*
* @example
* var o = {
* "short_description": "Test Incident",
* "description": "This is a test incident"
* }
* client.insert("incident",o,function(error,result) {
* if(!error) {
* // result contains array of inserted objets
* }
* });
*/
Client.prototype.insert = function(table,object,callback) {
var url = this.instance + "/" + table + ".do?JSONv2=&";
var params = querystring.stringify({
"sysparm_action": "insert"
});
var opts = {
body: JSON.stringify(object)
};
this.request.post(url + params, opts, function(err,response,body) {
restCallbackHandler(err,response,body,callback);
});
};
/**
* Deletes `object` from `table`. `object` may be either an Object containing a `sys_id` field or the `sys_id` itself.
*
* @param {string} table - the table to delete from
* @param {string|object} object - object or sys_id to delete
* @param {requestCallback} callback - response handler
*
* @example
* var o = {
* "sys_id: "9d385017c611228701d22104cc95c371"
* }
* client.delete("incident",o,function(error,result) {
* if(!error) {
* // result contains the object deleted
* }
* });
*/
Client.prototype.deleteRecord = function(table,object,callback) {
var sysid = object;
if(typeof object === 'object') {
sysid = object.sys_id;
}
var url = this.instance + "/" + table + ".do?JSONv2=&";
var params = querystring.stringify({
"sysparm_action": "deleteRecord"
});
var opts = {
body: JSON.stringify({
"sysparm_sys_id": sysid
})
};
this.request.post(url + params, opts, function(err,response,body) {
restCallbackHandler(err,response,body,callback);
});
};
/**
* Gets all records matching the `query`.
*
* @param {string} table - the table to delete from
* @param {string} query - [encoded query string](http://wiki.servicenow.com/index.php?title=Embedded:Encoded_Query_Strings) selecting the records for deletion
* @param {requestCallback} callback - response handler
*
* @example
* client.deleteMultiple("incident","short_description=deleteme",function(error,result) {
* if(!error) {
* // result contains an array of objects matching query
* }
* });
*/
Client.prototype.deleteMultiple = function(table,query,callback) {
var url = this.instance + "/" + table + ".do?JSONv2=&";
var params = querystring.stringify({
"sysparm_action": "deleteMultiple",
"sysparm_query" : query
});
this.request.get(url + params, function(err,response,body) {
restCallbackHandler(err,response,body,callback);
});
};
/**
* Standard handler for handling ServiceNow REST responses
* @private
*/
function restCallbackHandler(err,response,body,callback) {
var error = jsonServiceErrorProcessor(err,response,body);
if(error) {
callback(error);
return;
}
var o = JSON.parse(body);
callback(null,o);
}
/**
* Takes arguments from request callback and if any error result is found, returns the error
* Any per-record errors are returned as an array of __error objects with message and reason
* @private
*/
function jsonServiceErrorProcessor(error,response,body) {
if(error) return error;
if(response.statusCode!=200) return body;
var o = JSON.parse(body);
if(o.error) return o.error;
if(o.records) {
var errors = [];
for(var i in o.records) {
if(i.__error) errors.push(i.__error);
}
if(errors.length>0) {
return errors;
}
}
return null;
}
module.exports.Client = Client;