Skip to content

Commit 56bf277

Browse files
authored
Iamshankha/auto tag incident feature (#1927)
* 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] * Create README.md 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 ad444be commit 56bf277

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-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);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
📘 Overview
2+
3+
This feature automatically scans newly created or updated Incidents for specific keywords (like Email, VPN, Server) and attaches structured Labels automatically wheneven any incident gets created.
4+
It leverages ServiceNow’s native label and label_entry tables to organize incidents into searchable categories — improving visibility, reporting, and problem trend analysis.
5+
6+
🎯 Problem Statement
7+
8+
Manual tagging of incidents is inconsistent and time-consuming. As a result, identifying recurring issues or related incidents becomes difficult.
9+
The Incident Auto-Tagging solution automates this process, ensuring every incident containing common keywords is automatically labeled and linked for faster triage and analytics.
10+
11+
💡 Solution Approach
12+
13+
A Business Rule runs on the Incident table (Before Insert) to:
14+
15+
Detect defined keywords from Short Description and Description.
16+
17+
Create or reuse a Label (label table) for each keyword.
18+
19+
Create a Label Entry (label_entry table) linking the Label to the Incident with details such as title, URL, table, etc.
20+
21+
Prevent duplicates by verifying existing entries before insertion.
22+
23+
🧩 Benefits
24+
25+
🕒 Time saving: Minimizes time as no manual tagging is needed
26+
27+
🔍 Quick Search: Filter incidents by keyword-based labels.
28+
29+
⚡ Faster Resolution: Group recurring issues for faster response.
30+
31+
📊 Analytics Ready: Enables trend and problem management reporting.
32+
33+
🧠 Reusable Logic: Extendable to Change Requests or Catalog Items.

0 commit comments

Comments
 (0)