Skip to content

Commit 3efc2b1

Browse files
authored
Hashtag & Mention Extractor for ServiceNow v2 (#2088)
* Catalog Item Explorer - Widget Update Functionality update: - Support for the external URL content items. - The default target window changed to "_self" (same window). - Option to open an item in a new window added at the end of the row. * Update css.scss * Update script.js * Update client_script.js * Update options_schema.json * Create script.js * Create README.md * Rename README.md to README.md * Rename script.js to script.js * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update script.js * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md
1 parent dd8da67 commit 3efc2b1

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Hashtag & Mention Extractor for ServiceNow
2+
3+
A simple yet useful **ServiceNow Background Script** that extracts all hashtags (`#example`) and mentions (`@user`) from any text input using regular expressions.
4+
5+
---
6+
7+
### 💡 Example Use Cases
8+
- Automatically identify hashtags and mentions in **incident comments**, **knowledge articles**, or **survey feedback**.
9+
- Extract tags or mentions from user input in **Catalog Items/Record Producers** to populate hidden variables or drive logic.
10+
11+
---
12+
### 🚀 How to Run
13+
1. In your ServiceNow instance, navigate to **System Definition → Scripts – Background**.
14+
2. Paste the script from this repository and adjust it according to your needs.
15+
3. Click **Run Script**.
16+
17+
---
18+
19+
### 📦 Reusability
20+
The logic is **self-contained** within a single function block - no dependencies or external calls.
21+
You can easily **copy and adjust it** to fit different contexts:
22+
- Use it inside a **Business Rule**, **Script Include**, or **Flow Action Script** (see additional instructions below).
23+
- Replace the sample `demoData` with a field value (e.g., `current.description`) to analyze the data.
24+
- Adjust the regex to detect other patterns (emails, incident reference, etc.). See comments in the code for the additional examples.
25+
26+
---
27+
28+
### 🔧 Possible Extensions
29+
- Parse table data (`sys_journal_field`, `kb_knowledge`) instead of static text.
30+
- Store extracted tags in a custom table for analytics.
31+
32+
---
33+
34+
### ℹ️ Additional Instructions:
35+
#### Use in Script Include
36+
37+
1. Go to **Script Includes** in the Application Navigator.
38+
2. Click **New**, and name it (e.g., `TagExtractorUtils`).
39+
3. Set the following options:
40+
- **Client Callable**: `false`
41+
- **Accessible from**: `All application scopes`
42+
4. Paste this code:
43+
44+
```javascript
45+
var TagExtractorUtils = Class.create();
46+
TagExtractorUtils.prototype = {
47+
initialize: function () {},
48+
49+
extract: function (text) {
50+
var result = {
51+
hashtags: text.match(/#[A-Za-z0-9_]+/g),
52+
mentions: text.match(/@[A-Za-z0-9_]+/g)
53+
};
54+
return result;
55+
},
56+
57+
type: 'TagExtractorUtils'
58+
};
59+
```
60+
5. Use it as any other script include.
61+
62+
#### Use in Business Rule with a couple of custom text fields and previously created script include
63+
64+
1. Go to **Business Rules** in the Application Navigator.
65+
2. Click **New**, choose a table (e.g., `sc_task`, `incident`).
66+
3. Set **When** to `before`, `after`, or `async` (usually `async`).
67+
4. In the **Script** section, call your tag-extracting logic:
68+
69+
```javascript
70+
(function executeRule(current, previous /*null when async*/) {
71+
var text = current.short_description + ' ' + current.description;
72+
var tags = new TagExtractorUtils().extract(text);
73+
74+
current.u_hashtags = tags.hashtags.join(', ');
75+
current.u_mentions = tags.mentions.join(', ');
76+
77+
})(current, previous);
78+
```
79+
#### Use in Flow Action Script and previously created script include
80+
81+
1. Go to **Flow Designer > Action** and click **New Action**.
82+
2. Give it a name like `Extract Tags from Text`.
83+
3. Add an **Input** (e.g., `input_text` of type String).
84+
4. Add a **Script step**, then paste the script there:
85+
86+
```javascript
87+
(function execute(inputs, outputs) {
88+
var text = inputs.input_text || '';
89+
var tags = new TagExtractorUtils().extract(text);
90+
91+
outputs.hashtags = tags.hashtags.join(', ');
92+
outputs.mentions = tags.mentions.join(', ');
93+
})(inputs, outputs);
94+
```
95+
5. Use this **Action** in your flow to extract tags by passing the text to it as a parameter.
96+
97+
<sub>🤖 This contribution was partially created with the help of AI.</sub>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
(function() {
2+
3+
var demoData = "Quick test for hashtag and mention extraction in ServiceNow. " +
4+
"Let's make sure it catches #Hack4Good #ServiceNow #regex and mentions like @ivan and @servicenow.";
5+
6+
// ---------------------------------------------
7+
// Useful regex patterns (for future extension):
8+
// - Hashtags: /#[A-Za-z0-9_]+/g
9+
// - Mentions: /@[A-Za-z0-9_]+/g
10+
// - Emails: /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g
11+
// - Ticket refs: /INC\d{7}/g (e.g., INC0012345)
12+
// ---------------------------------------------
13+
14+
// Separate regex patterns for clarity
15+
var hashtagRegex = /#[A-Za-z0-9_]+/g;
16+
var mentionRegex = /@[A-Za-z0-9_]+/g;
17+
18+
// Match both types
19+
var hashtags = demoData.match(hashtagRegex);
20+
var mentions = demoData.match(mentionRegex);
21+
22+
if (hashtags.length > 0) {
23+
gs.info('Found ' + hashtags.length + ' hashtags: ' + hashtags.join(', '));
24+
} else {
25+
gs.info('No hashtags found.');
26+
}
27+
28+
if (mentions.length > 0) {
29+
gs.info('Found ' + mentions.length + ' mentions: ' + mentions.join(', '));
30+
} else {
31+
gs.info('No mentions found.');
32+
}
33+
34+
})();

0 commit comments

Comments
 (0)