Created
February 15, 2018 12:07
-
-
Save cmcdevitt/4257503f6a0fae9b420afb0ffed5a7cd to your computer and use it in GitHub Desktop.
ServiceNow Scripted RESTAPI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // POST /api/auclg/v1/si/security_incidents | |
| // Content-Type application/json Accept application/json | |
| (function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) { | |
| //Creat Security Incident | |
| var caller; | |
| var cmdb_ci; | |
| var assignmentGroup; | |
| var shor_description; | |
| var requestBody = request.body; | |
| var requestData = requestBody.data; | |
| if(requestData instanceof Array){ | |
| //test for multiple body objects / JSON To-do loop through incoming object? | |
| caller = requestData[0].caller; | |
| cmdb_ci = requestData[0].cmdb_ci; | |
| short_description = requestData[0].short_description; | |
| u_rq_ticket_id = requestData[0].u_rq_ticket_id; | |
| u_rq_caller = requestData[0].u_rq_caller; | |
| u_rq_source = requestData[0].u_rq_source; | |
| u_rq_priority = requestData[0].u_rq_priority; | |
| u_rq_category = requestData[0].u_rq_category; | |
| u_rq_subcategory = requestData[0].u_rq_subcategory; | |
| u_rq_close_code = requestData[0].u_rq_close_code; | |
| u_rq_opened = requestData[0].u_rq_opened; | |
| u_rq_assignment_group = requestData[0].u_rq_assignment_group; | |
| u_rq_assigned_to = requestData[0].u_rq_assigned_to; | |
| u_rq_child_ticket = requestData[0].u_rq_child_ticket; | |
| u_rq_detection_source = requestData[0].u_rq_detection_source; | |
| u_rq_close_by = requestData[0].u_rq_close_by; | |
| }else{ | |
| caller = requestData.caller; | |
| cmdb_ci = requestData.cmdb_ci; | |
| short_description = requestData.short_description; | |
| u_rq_ticket_id = requestData.u_rq_ticket_id; | |
| u_rq_caller = requestData.u_rq_caller; | |
| u_rq_source = requestData.u_rq_source; | |
| u_rq_priority = requestData.u_rq_priority; | |
| u_rq_category = requestData.u_rq_category; | |
| u_rq_subcategory = requestData.u_rq_subcategory; | |
| u_rq_close_code = requestData.u_rq_close_code; | |
| u_rq_opened = requestData.u_rq_opened; | |
| u_rq_assignment_group = requestData.u_rq_assignment_group; | |
| u_rq_assigned_to = requestData.u_rq_assigned_to; | |
| u_rq_child_ticket = requestData.u_rq_child_ticket; | |
| u_rq_detection_source = requestData.u_rq_detection_source; | |
| u_rq_close_by = requestData.u_rq_close_by; | |
| } | |
| //Test ACG manadatory fields | |
| //Manadatory: Caller, Configuration Item, Short Description | |
| //ToDo Caller should be an ACG account | |
| var missingMandatoryFields = []; | |
| if(caller === ""){ | |
| missingMandatoryFields.push('caller'); | |
| } | |
| if (cmdb_ci === ""){ | |
| missingMandatoryFields.push('cmdb_ci'); | |
| } | |
| if (short_description === ""){ | |
| missingMandatoryFields.push('short_description'); | |
| } | |
| if(missingMandatoryFields != 0){ | |
| var errorString = 'The requst is missing the following manadatory field(s): '; | |
| missingMandatoryFields.forEach(function(element) { | |
| errorString += element + " , "; | |
| }); | |
| return new sn_ws_err.BadRequestError(errorString); | |
| } | |
| //Create Security Incident | |
| var incidentSysID = ''; | |
| try{ | |
| var gr = new GlideRecord('sn_si_incident'); | |
| gr.initialize(); | |
| gr.caller = caller; | |
| //gr.cmdb_ci = '006689dc6ff70900f71de82fae3ee4f5'; todo turn text in to sys_id? | |
| //gr.cmdb_ci = cmdb_ci; | |
| gr.cmdb_ci.setDisplayValue(cmdb_ci); | |
| gr.short_description = short_description; | |
| gr.u_rq_ticket_id = u_rq_ticket_id; | |
| gr.u_rq_caller = u_rq_caller; | |
| gr.u_rq_source = u_rq_source; | |
| gr.u_rq_priority = u_rq_priority; | |
| gr.u_rq_category = u_rq_category; | |
| gr.u_rq_subcategory = u_rq_subcategory; | |
| gr.u_rq_close_code = u_rq_close_code; | |
| gr.u_rq_opened = u_rq_opened; | |
| gr.u_rq_assignment_group = u_rq_assignment_group; | |
| gr.u_rq_assigned_to = u_rq_assigned_to; | |
| gr.u_rq_child_ticket = u_rq_child_ticket; | |
| gr.u_rq_detection_source = u_rq_detection_source; | |
| gr.u_rq_close_by = u_rq_close_by; | |
| //do it and grab sys_id | |
| incidentSysID = gr.insert(); | |
| } catch(e){ | |
| var error = new sn_ws_err.ServiceError(); | |
| error.setStatus(500); | |
| error.setDetail("Create Record Failed"); | |
| error.setMessage("Failed to create:" + short_description); | |
| gs.log("Failed to create Security Incident: " + e.message); | |
| return error; | |
| } | |
| //Loookup newely created Security Incident so we can send it back in the response | |
| var incidentNumber = ''; | |
| try{ | |
| var grlu = new GlideRecord('sn_si_incident'); | |
| grlu.initialize(); | |
| grlu.addQuery('sys_id','=',incidentSysID); | |
| grlu.query(); | |
| if(grlu.next()){ | |
| //gs.log("Inside next()" + grlu.number + " : " + grlu); | |
| var incNum = {"Security Incident: " : grlu.number}; | |
| return incNum; | |
| }else{ | |
| response.setStatus(201); | |
| return incidentSysID; | |
| } | |
| }catch(e){ | |
| var error = new sn_ws_err.ServiceError(); | |
| error.setStatus(500); | |
| error.setDetail("Failed to find newely created Security Incident"); | |
| error.setMessage("Failed to find:" + short_description); | |
| gs.log("Failed to find sys_id: " + incidentSysID + " : " + e.message); | |
| return error; | |
| } | |
| })(request, response); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment