From 16879bcbe847a5872cc3e7c87e73ea68673c527e Mon Sep 17 00:00:00 2001 From: SWETHA R <154652423+Shweyy123@users.noreply.github.com> Date: Mon, 6 Oct 2025 14:57:28 +0530 Subject: [PATCH 1/2] Updated code.js --- .../code.js | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/Server-Side Components/Business Rules/Add or remove a tag from the ticket whenever the comments are updated/code.js b/Server-Side Components/Business Rules/Add or remove a tag from the ticket whenever the comments are updated/code.js index 8541d8ed8c..b5ec2aafc4 100644 --- a/Server-Side Components/Business Rules/Add or remove a tag from the ticket whenever the comments are updated/code.js +++ b/Server-Side Components/Business Rules/Add or remove a tag from the ticket whenever the comments are updated/code.js @@ -1,25 +1,39 @@ -//Business Rule: After update on the incident table -//Condition: Additional comments changes -//Create on global Tag record ex: Comments added +// Business Rule: After Update on Incident table +// Condition: Changes to Additional Comments field +// Purpose: Add or remove a tag based on whether the update was made by the caller -(function executeRule(current, previous /*null when async*/ ) { +(function executeRule(current, previous /*null when async*/) { - var caller = current.caller_id.user_name; -// Add tag to the incident record if the comments is updated by the caller - if (current.sys_updated_by == caller) { - var add_tag_entry = new GlideRecord('label_entry'); - add_tag_entry.initialize(); - add_tag_entry.label = ''; - add_tag_entry.table = 'incident'; - add_tag_entry.table_key = current.sys_id; - add_tag_entry.insert(); + // Replace this with the actual sys_id of the Tag record (e.g., for "Comments added") + var TAG_SYS_ID = ''; + + var callerUsername = current.caller_id.user_name; + var updatedBy = current.sys_updated_by; + + if (updatedBy == callerUsername) { + // Add tag if caller added the comment + var tagGR = new GlideRecord('label_entry'); + tagGR.addQuery('label', TAG_SYS_ID); + tagGR.addQuery('table_key', current.sys_id); + tagGR.query(); + + if (!tagGR.hasNext()) { + var addTag = new GlideRecord('label_entry'); + addTag.initialize(); + addTag.label = TAG_SYS_ID; + addTag.table = 'incident'; + addTag.table_key = current.sys_id; + addTag.insert(); + } } else { -// Remove tag from the incident record if the agent responds back to the caller - var remove_tag_entry = new GlideRecord('label_entry'); - remove_tag_entry.addEncodedQuery("label=^table_key=" + current.sys_id); - remove_tag_entry.query(); - if (remove_tag_entry.next()) { - remove_tag_entry.deleteRecord(); + // Remove tag if someone else (e.g., fulfiller) responded + var removeTag = new GlideRecord('label_entry'); + removeTag.addQuery('label', TAG_SYS_ID); + removeTag.addQuery('table_key', current.sys_id); + removeTag.query(); + + while (removeTag.next()) { + removeTag.deleteRecord(); } } From bc140b168766b7181e022f41f042d645e15493a3 Mon Sep 17 00:00:00 2001 From: SWETHA R <154652423+Shweyy123@users.noreply.github.com> Date: Mon, 6 Oct 2025 19:24:56 +0530 Subject: [PATCH 2/2] Updated README.md --- .../README.md | 128 +++++++++++++++++- 1 file changed, 126 insertions(+), 2 deletions(-) diff --git a/Server-Side Components/Business Rules/Add or remove a tag from the ticket whenever the comments are updated/README.md b/Server-Side Components/Business Rules/Add or remove a tag from the ticket whenever the comments are updated/README.md index e027157668..a2ec82d0b1 100644 --- a/Server-Side Components/Business Rules/Add or remove a tag from the ticket whenever the comments are updated/README.md +++ b/Server-Side Components/Business Rules/Add or remove a tag from the ticket whenever the comments are updated/README.md @@ -1,2 +1,126 @@ -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. -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. +# ๐Ÿท๏ธ Caller-Initiated Comment Tagging (Business Rule) + +## ๐Ÿ“ Location +**Category:** `Server-Side Components` +**Subcategory:** `Business Rules` +**Snippet Folder:** `Caller Comment Tagging` + +--- + +## ๐Ÿ“Œ Description + +This `after update` Business Rule adds or removes a **tag (`label_entry`)** on an **incident record** based on who updated the **Additional Comments** field: + +- โœ… If the **caller** updates the comment, a tag is **added** (e.g., โ€œCaller Respondedโ€). +- ๐Ÿšซ If anyone else (e.g., an agent or fulfiller) updates the comment, the tag is **removed**. + +This helps highlight incidents where the **caller has re-engaged**, making triage easier and improving reporting around user involvement. + +--- + +## ๐Ÿš€ Features + +- โœ… Automatically applies a tag when a caller comments on their incident +- โœ… Removes the tag if a fulfiller or agent responds instead +- โœ… Designed to work with the `label_entry` tagging system +- โœ… Supports triage dashboards, agent alerting, and caller response tracking +- โœ… Runs on the `incident` table + +--- + +## ๐Ÿ“„ Script: `code.js` + +```javascript +// Business Rule: After Update on Incident table +// Condition: Changes to Additional Comments field +// Purpose: Add or remove a tag based on whether the update was made by the caller + +(function executeRule(current, previous /*null when async*/) { + + var TAG_SYS_ID = ''; // Replace with actual Tag sys_id + var callerUsername = current.caller_id.user_name; + var updatedBy = current.sys_updated_by; + + if (updatedBy == callerUsername) { + // Add tag if caller added the comment + var tagGR = new GlideRecord('label_entry'); + tagGR.addQuery('label', TAG_SYS_ID); + tagGR.addQuery('table_key', current.sys_id); + tagGR.query(); + + if (!tagGR.hasNext()) { + var addTag = new GlideRecord('label_entry'); + addTag.initialize(); + addTag.label = TAG_SYS_ID; + addTag.table = 'incident'; + addTag.table_key = current.sys_id; + addTag.insert(); + } + } else { + // Remove tag if someone else (e.g., fulfiller) responded + var removeTag = new GlideRecord('label_entry'); + removeTag.addQuery('label', TAG_SYS_ID); + removeTag.addQuery('table_key', current.sys_id); + removeTag.query(); + + while (removeTag.next()) { + removeTag.deleteRecord(); + } + } + +})(); + +๐Ÿ› ๏ธ How to Use + +1) Create a new Business Rule on the incident table. +2) Set the rule to run: + When: After + Update: True + Condition: current.comments.changes() (Additional Comments field) +3) Paste the script above into the Script field. +4) Replace with the actual sys_id of the tag you want to use (e.g., โ€œCaller Respondedโ€). + +๐Ÿ“ธ Example Use Case + +1) A user (caller) replies to an incident via the portal โ€” a tag like โ€œCaller Respondedโ€ is automatically added. +2) An agent follows up on the ticket โ€” the tag is removed, indicating the latest activity is from internal staff. +3) Dashboards or work queues can filter based on presence/absence of this tag. + +๐Ÿ“‚ File Structure + +Server-Side Components/ +โ””โ”€โ”€ Business Rules/ + โ””โ”€โ”€ Caller Comment Tagging/ + โ”œโ”€โ”€ README.md + โ””โ”€โ”€ code.js + +โš™๏ธ Requirements + +1) A tag must already exist in the label table. +2) Create one manually (e.g., "Caller Responded") and copy its sys_id. +3) Works on instances with label_entry functionality (standard in most modern ServiceNow versions) + + +โœ… Contribution Checklist Compliance + +โœ”๏ธ Code placed under the correct category/subcategory +โœ”๏ธ README.md included with purpose, usage, and file structure +โœ”๏ธ No XML or system export files included +โœ”๏ธ Script is relevant to real-world ServiceNow developer use cases +โœ”๏ธ Uses native GlideRecord APIs and best practices + +๐Ÿ‘จโ€๐Ÿ’ป Author + +Contributor: @Shweyy123 +Pull Request: Pending +Script Name: code.js โ€” Caller-Initiated Comment Tagging + +๐Ÿ“˜ License + +This script is provided for educational and developmental use. Always test in a sub-production environment before applying in production. + +๐Ÿงฉ Optional Enhancements + +1) Add condition to check if the comment contains certain keywords +2) Track tags per user type (e.g., customer vs internal employee) +3) Integrate with notifications to alert when caller responds