|
1 | | -The purpose of this code is to conditionally apply a specific label (label_entry) to an Incident when the person updating the record is the same as the caller. If the update is made by someone else, the label is removed. |
2 | | -This mechanism helps fulfillers quickly identify caller driven updates, enabling faster and more targeted responses. Additionally, it can be leveraged in reporting to track caller engagement. |
| 1 | +# 🏷️ Caller-Initiated Comment Tagging (Business Rule) |
| 2 | + |
| 3 | +## 📁 Location |
| 4 | +**Category:** `Server-Side Components` |
| 5 | +**Subcategory:** `Business Rules` |
| 6 | +**Snippet Folder:** `Caller Comment Tagging` |
| 7 | + |
| 8 | +--- |
| 9 | + |
| 10 | +## 📌 Description |
| 11 | + |
| 12 | +This `after update` Business Rule adds or removes a **tag (`label_entry`)** on an **incident record** based on who updated the **Additional Comments** field: |
| 13 | + |
| 14 | +- ✅ If the **caller** updates the comment, a tag is **added** (e.g., “Caller Responded”). |
| 15 | +- 🚫 If anyone else (e.g., an agent or fulfiller) updates the comment, the tag is **removed**. |
| 16 | + |
| 17 | +This helps highlight incidents where the **caller has re-engaged**, making triage easier and improving reporting around user involvement. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +## 🚀 Features |
| 22 | + |
| 23 | +- ✅ Automatically applies a tag when a caller comments on their incident |
| 24 | +- ✅ Removes the tag if a fulfiller or agent responds instead |
| 25 | +- ✅ Designed to work with the `label_entry` tagging system |
| 26 | +- ✅ Supports triage dashboards, agent alerting, and caller response tracking |
| 27 | +- ✅ Runs on the `incident` table |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 📄 Script: `code.js` |
| 32 | + |
| 33 | +```javascript |
| 34 | +// Business Rule: After Update on Incident table |
| 35 | +// Condition: Changes to Additional Comments field |
| 36 | +// Purpose: Add or remove a tag based on whether the update was made by the caller |
| 37 | + |
| 38 | +(function executeRule(current, previous /*null when async*/) { |
| 39 | + |
| 40 | + var TAG_SYS_ID = '<sys_id_of_the_Tag>'; // Replace with actual Tag sys_id |
| 41 | + var callerUsername = current.caller_id.user_name; |
| 42 | + var updatedBy = current.sys_updated_by; |
| 43 | + |
| 44 | + if (updatedBy == callerUsername) { |
| 45 | + // Add tag if caller added the comment |
| 46 | + var tagGR = new GlideRecord('label_entry'); |
| 47 | + tagGR.addQuery('label', TAG_SYS_ID); |
| 48 | + tagGR.addQuery('table_key', current.sys_id); |
| 49 | + tagGR.query(); |
| 50 | + |
| 51 | + if (!tagGR.hasNext()) { |
| 52 | + var addTag = new GlideRecord('label_entry'); |
| 53 | + addTag.initialize(); |
| 54 | + addTag.label = TAG_SYS_ID; |
| 55 | + addTag.table = 'incident'; |
| 56 | + addTag.table_key = current.sys_id; |
| 57 | + addTag.insert(); |
| 58 | + } |
| 59 | + } else { |
| 60 | + // Remove tag if someone else (e.g., fulfiller) responded |
| 61 | + var removeTag = new GlideRecord('label_entry'); |
| 62 | + removeTag.addQuery('label', TAG_SYS_ID); |
| 63 | + removeTag.addQuery('table_key', current.sys_id); |
| 64 | + removeTag.query(); |
| 65 | + |
| 66 | + while (removeTag.next()) { |
| 67 | + removeTag.deleteRecord(); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | +})(); |
| 72 | + |
| 73 | +🛠️ How to Use |
| 74 | + |
| 75 | +1) Create a new Business Rule on the incident table. |
| 76 | +2) Set the rule to run: |
| 77 | + When: After |
| 78 | + Update: True |
| 79 | + Condition: current.comments.changes() (Additional Comments field) |
| 80 | +3) Paste the script above into the Script field. |
| 81 | +4) Replace <sys_id_of_the_Tag> with the actual sys_id of the tag you want to use (e.g., “Caller Responded”). |
| 82 | + |
| 83 | +📸 Example Use Case |
| 84 | + |
| 85 | +1) A user (caller) replies to an incident via the portal — a tag like “Caller Responded” is automatically added. |
| 86 | +2) An agent follows up on the ticket — the tag is removed, indicating the latest activity is from internal staff. |
| 87 | +3) Dashboards or work queues can filter based on presence/absence of this tag. |
| 88 | + |
| 89 | +📂 File Structure |
| 90 | + |
| 91 | +Server-Side Components/ |
| 92 | +└── Business Rules/ |
| 93 | + └── Caller Comment Tagging/ |
| 94 | + ├── README.md |
| 95 | + └── code.js |
| 96 | + |
| 97 | +⚙️ Requirements |
| 98 | + |
| 99 | +1) A tag must already exist in the label table. |
| 100 | +2) Create one manually (e.g., "Caller Responded") and copy its sys_id. |
| 101 | +3) Works on instances with label_entry functionality (standard in most modern ServiceNow versions) |
| 102 | + |
| 103 | + |
| 104 | +✅ Contribution Checklist Compliance |
| 105 | + |
| 106 | +✔️ Code placed under the correct category/subcategory |
| 107 | +✔️ README.md included with purpose, usage, and file structure |
| 108 | +✔️ No XML or system export files included |
| 109 | +✔️ Script is relevant to real-world ServiceNow developer use cases |
| 110 | +✔️ Uses native GlideRecord APIs and best practices |
| 111 | + |
| 112 | +👨💻 Author |
| 113 | + |
| 114 | +Contributor: @Shweyy123 |
| 115 | +Pull Request: Pending |
| 116 | +Script Name: code.js — Caller-Initiated Comment Tagging |
| 117 | + |
| 118 | +📘 License |
| 119 | + |
| 120 | +This script is provided for educational and developmental use. Always test in a sub-production environment before applying in production. |
| 121 | + |
| 122 | +🧩 Optional Enhancements |
| 123 | + |
| 124 | +1) Add condition to check if the comment contains certain keywords |
| 125 | +2) Track tags per user type (e.g., customer vs internal employee) |
| 126 | +3) Integrate with notifications to alert when caller responds |
0 commit comments