Skip to content

Commit a6ec3f5

Browse files
JSON Mapping for Incident Creation (#1858)
1 parent 126f76c commit a6ec3f5

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
function createIncidentsFromJSON(payload) {
2+
var data = JSON.parse(payload);
3+
// Check if Data is Array or not, If not then add into array
4+
var dataArray = Array.isArray(data) ? data : [data];
5+
6+
7+
dataArray.forEach(function(item) {
8+
var gr = new GlideRecord('incident');
9+
gr.initialize();
10+
11+
for (var key in item) {
12+
if (gr.isValidField(key)) {
13+
gr[key] = item[key];
14+
}
15+
}
16+
var incidentSysId = gr.insert();
17+
gs.info("Incident created with Sys ID: " + incidentSysId);
18+
});
19+
}
20+
21+
// Usage with a single object
22+
var singlePayload = '{"short_description":"System Down","caller_id":"durgesh1@example.com","priority":1,"assignment_group":"IT Support"}';
23+
createIncidentsFromJSON(singlePayload);
24+
25+
// Usage with an array of objects
26+
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"}]';
27+
createIncidentsFromJSON(arrayPayload);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# JSON Mapping for Incident Creation
2+
3+
Usecase -
4+
5+
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.

0 commit comments

Comments
 (0)