Skip to content

Commit 7708c3a

Browse files
authored
Create Auto_Tag_Incident.js
Incident Auto-Tagging Automatically detects keywords like email, VPN, or server from the incident’s short description and description. Creates a corresponding Label (if not already present) and a Label Entry linked to that incident to fulfill the requirement of auto tagging. Benefit: Helps automatically tag incidents with relevant keywords, improving searchability, categorization, and visibility across the platform. Type: Before Business Rule Table: Incident [incident]
1 parent a6d1a65 commit 7708c3a

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
(function executeRule(current, previous /*null when async*/) {
2+
3+
// ==============================================================
4+
// Business Rule: Incident Auto-Tagging
5+
// Table: Incident
6+
// --------------------------------------------------------------
7+
// Purpose:
8+
// Automatically detect keywords (like "email", "vpn", "server")
9+
// from the Incident’s short description and description fields.
10+
//
11+
// Based on detected keywords:
12+
// - A corresponding Label is created (if not already present)
13+
// - A related Label Entry is created for that Incident
14+
//
15+
// 💡 Benefit:
16+
// Adding Labels and Label Entries helps automatically tag
17+
// incidents with the right keywords — improving searchability,
18+
// categorization, and visibility across the platform.
19+
// ==============================================================
20+
21+
// Step 1: Collect tags from the description
22+
var tags = [];
23+
var desc = (current.short_description + " " + current.description).toLowerCase();
24+
25+
if (desc.indexOf("email") > -1) tags.push("Email");
26+
if (desc.indexOf("vpn") > -1) tags.push("VPN");
27+
if (desc.indexOf("server") > -1) tags.push("Server");
28+
29+
// Step 2: Process each detected tag
30+
for (var i = 0; i < tags.length; i++) {
31+
32+
var tagName = tags[i]; // e.g., "Email"
33+
var labelSysId;
34+
35+
// Step 2.1: Check if Label already exists
36+
var labelGR = new GlideRecord("label");
37+
labelGR.addQuery("name", tagName);
38+
labelGR.query();
39+
40+
if (labelGR.next()) {
41+
// Label exists — reuse it
42+
labelSysId = labelGR.sys_id.toString();
43+
} else {
44+
// Create new Label
45+
var newLabel = new GlideRecord("label");
46+
newLabel.initialize();
47+
newLabel.name = tagName;
48+
newLabel.owner = gs.getUserID(); // Logged-in user sys_id
49+
newLabel.viewable_by = "everyone"; // Backend value of choice
50+
newLabel.active = true;
51+
labelSysId = newLabel.insert();
52+
}
53+
54+
// Step 2.2: Create Label Entry if not already present
55+
var entryGR = new GlideRecord("label_entry");
56+
entryGR.addQuery("table_key", current.sys_id);
57+
entryGR.addQuery("label", labelSysId);
58+
entryGR.query();
59+
60+
if (!entryGR.hasNext()) {
61+
62+
var newEntry = new GlideRecord("label_entry");
63+
newEntry.initialize();
64+
newEntry.title = "Incident - " + current.number; // e.g., Incident - INC0010003
65+
newEntry.label = labelSysId;
66+
newEntry.url = "incident.do?sys_id=" + current.sys_id + "&sysparm_view=";
67+
newEntry.table = "incident";
68+
newEntry.id_type = "Incident";
69+
newEntry.table_key = current.sys_id;
70+
newEntry.read = "yes";
71+
newEntry.insert();
72+
}
73+
}
74+
75+
})(current, previous);

0 commit comments

Comments
 (0)