Skip to content

Commit 854130e

Browse files
authored
Update ci (#887)
* Create UpdateCi.js Scripted rest resource to update the CI * Update CmdbApi.js Created a function for Scripted Rest API to update the CIs * Update readme.md Updated the API details for the API to update the CI
1 parent 41b7a89 commit 854130e

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

Scripted REST Api/CMDB API/CmdbApi.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,73 @@ createCiRelationship: function() {
320320
});
321321

322322
},
323+
324+
/**
325+
* Update a Configuration Item (CI).
326+
* Mapped to PATCH /cis/{ci_type}/{sys_id}
327+
*
328+
* This function updates a CI based on its type and system ID.
329+
*
330+
* @returns {Object} JSON response containing the updated CI or error details.
331+
* @throws {BadRequestError} If an invalid CI type is provided.
332+
* @throws {ServiceError} If the operation fails for any reason.
333+
* @throws {NotFoundError} If no record is found for the provided system ID.
334+
*/
335+
updateCi: function() {
336+
var self = this;
337+
338+
var ciType = self.getPathParam('ci_type', '');
339+
340+
var ciSysId = self.getPathParam('sys_id');
341+
342+
var payload = self.body;
343+
344+
if (!self._isValidCiType(ciType)) {
345+
return sn_ws_err.BadRequestError('Invalid CI Type provided:' + ciType);
346+
}
347+
348+
var ciRec = new GlideRecord(ciType);
349+
if (ciRec.get(ciSysId)) {
350+
351+
try {
352+
for (var key in payload) {
353+
if (ciRec.isValidField(key) && key.indexOf('sys_') != 0) {
354+
var value = payload[key];
355+
var el = ciRec.getElement(key);
356+
357+
var descriptor = el.getED();
358+
var choice = descriptor.choice;
359+
var type = descriptor.getInternalType();
360+
361+
if (choice != '0') {
362+
ciRec[key] = self._getChoiceValue(ciType, key, value);
363+
} else if (type == 'reference') {
364+
ciRec[key] = self._getReferenceValue(ciType, key, value);
365+
} else {
366+
ciRec[key] = payload[key];
367+
}
368+
}
369+
}
370+
ciRec.update();
371+
372+
var errMsg = ciRec.getLastErrorMessage();
373+
if (!gs.nil(errMsg)) {
374+
return new sn_ws_err.BadRequestError(errMsg);
375+
}
376+
377+
return self._getGrResultStream(ciType, ciRec.getValue('sys_id'), {});
378+
379+
} catch (e) {
380+
var serverError = new sn_ws_err.ServiceError();
381+
serverError.setStatus(500);
382+
serverError.setMessage('Operation Failed');
383+
serverError.setDetail(e);
384+
return serverError;
385+
}
386+
} else {
387+
return new sn_ws_err.NotFoundError('No record found');
388+
}
389+
},
323390

324391
/**
325392
* Determine the type of source ID.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
2+
3+
// implement resource here
4+
return new CmdbApi(request, response).updateCi();
5+
6+
})(request, response);

Scripted REST Api/CMDB API/readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Table of Contents
99
5. Retrieve CI Group
1010
6. Retrieve CI Relationship types
1111
7. Retrieve CIs
12+
8. Update CI
1213

1314
1. Create CIs
1415
This API is designed to create Configuration Items (CIs).
@@ -114,3 +115,18 @@ Table of Contents
114115
Response:
115116
Status Code: 200 OK
116117
Response Body: JSON Details of CIs based on the given type
118+
119+
8. Update CIs
120+
This API is designed to Update a Configuration Item (CI).
121+
Request Details:
122+
Type: HTTP
123+
Method: PATCH
124+
URI: https://<service-now-domain>.service-now.com/api/cis/{ci_type}/{sys_id}
125+
Headers:
126+
Accept: application/json
127+
Content-Type: application/json
128+
Request Body:
129+
The request body should contain a JSON key-value pair for the required fields.
130+
Response:
131+
Status Code: 200 OK
132+
Response Body: JSON with required fields

0 commit comments

Comments
 (0)