|
| 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