From f3c08b1291924b7008e623a806994327e33ef4a1 Mon Sep 17 00:00:00 2001 From: Durgesh ahire Date: Mon, 6 Oct 2025 18:07:38 +0530 Subject: [PATCH] JSON Mapping for Incident Creation --- .../JsonMapping.js | 27 +++++++++++++++++++ .../README.md | 5 ++++ 2 files changed, 32 insertions(+) create mode 100644 Server-Side Components/Script Includes/JSON Mapping for Incident Creation/JsonMapping.js create mode 100644 Server-Side Components/Script Includes/JSON Mapping for Incident Creation/README.md diff --git a/Server-Side Components/Script Includes/JSON Mapping for Incident Creation/JsonMapping.js b/Server-Side Components/Script Includes/JSON Mapping for Incident Creation/JsonMapping.js new file mode 100644 index 0000000000..0a850ecd76 --- /dev/null +++ b/Server-Side Components/Script Includes/JSON Mapping for Incident Creation/JsonMapping.js @@ -0,0 +1,27 @@ +function createIncidentsFromJSON(payload) { + var data = JSON.parse(payload); + // Check if Data is Array or not, If not then add into array + var dataArray = Array.isArray(data) ? data : [data]; + + + dataArray.forEach(function(item) { + var gr = new GlideRecord('incident'); + gr.initialize(); + + for (var key in item) { + if (gr.isValidField(key)) { + gr[key] = item[key]; + } + } + var incidentSysId = gr.insert(); + gs.info("Incident created with Sys ID: " + incidentSysId); + }); +} + +// Usage with a single object +var singlePayload = '{"short_description":"System Down","caller_id":"durgesh1@example.com","priority":1,"assignment_group":"IT Support"}'; +createIncidentsFromJSON(singlePayload); + +// Usage with an array of objects +var arrayPayload = '[{"short_description":"System Down","caller_id":"durgesh2@example.com","priority":1,"assignment_group":"IT Support"}, {"short_description":"Email Issue","caller_id":"durgesh3@example.com","priority":2,"assignment_group":"IT Support"}]'; +createIncidentsFromJSON(arrayPayload); \ No newline at end of file diff --git a/Server-Side Components/Script Includes/JSON Mapping for Incident Creation/README.md b/Server-Side Components/Script Includes/JSON Mapping for Incident Creation/README.md new file mode 100644 index 0000000000..c3c92b1013 --- /dev/null +++ b/Server-Side Components/Script Includes/JSON Mapping for Incident Creation/README.md @@ -0,0 +1,5 @@ +# JSON Mapping for Incident Creation + +Usecase - + +When we got the JSON payload (Object/Array of Object) for Incident creation with dynamic fields. You need a reusable script to create an Incident without hardcoding fields.