Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
aa77acf
Catalog Item Explorer - Widget Update
ivan-betev Oct 10, 2025
ed18c98
Update css.scss
ivan-betev Oct 10, 2025
562f55d
Update script.js
ivan-betev Oct 10, 2025
465b76d
Update client_script.js
ivan-betev Oct 10, 2025
9f771b3
Update options_schema.json
ivan-betev Oct 10, 2025
7b4a419
Merge branch 'ServiceNowDevProgram:main' into main
ivan-betev Oct 10, 2025
f47ea54
Create script.js
ivan-betev Oct 12, 2025
32f3c77
Create README.md
ivan-betev Oct 12, 2025
07826f2
Rename README.md to README.md
ivan-betev Oct 12, 2025
6f2cc7f
Rename script.js to script.js
ivan-betev Oct 12, 2025
b88c0f3
Update README.md
ivan-betev Oct 12, 2025
772dcb8
Update README.md
ivan-betev Oct 12, 2025
2e9bf6c
Update README.md
ivan-betev Oct 12, 2025
5fa7c78
Update README.md
ivan-betev Oct 12, 2025
935dfd8
Update README.md
ivan-betev Oct 12, 2025
f8f7b02
Update script.js
ivan-betev Oct 12, 2025
42285df
Update README.md
ivan-betev Oct 12, 2025
6d4d166
Update README.md
ivan-betev Oct 12, 2025
63bb026
Update README.md
ivan-betev Oct 12, 2025
0ff1111
Update README.md
ivan-betev Oct 12, 2025
29742f3
Update README.md
ivan-betev Oct 12, 2025
4e1efc2
Update README.md
ivan-betev Oct 12, 2025
c860d7b
Update README.md
ivan-betev Oct 12, 2025
ad90ef5
Update README.md
ivan-betev Oct 12, 2025
fa8c5f2
Update README.md
ivan-betev Oct 12, 2025
6c483b2
Update README.md
ivan-betev Oct 12, 2025
f8a0fab
Update README.md
ivan-betev Oct 12, 2025
f43b543
Update README.md
ivan-betev Oct 12, 2025
5b72602
Update README.md
ivan-betev Oct 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Hashtag & Mention Extractor for ServiceNow

A simple yet useful **ServiceNow Background Script** that extracts all hashtags (`#example`) and mentions (`@user`) from any text input using regular expressions.

---

### 💡 Example Use Cases
- Automatically identify hashtags and mentions in **incident comments**, **knowledge articles**, or **survey feedback**.
- Extract tags or mentions from user input in **Catalog Items/Record Producers** to populate hidden variables or drive logic.

---
### 🚀 How to Run
1. In your ServiceNow instance, navigate to **System Definition → Scripts – Background**.
2. Paste the script from this repository and adjust it according to your needs.
3. Click **Run Script**.

---

### 📦 Reusability
The logic is **self-contained** within a single function block - no dependencies or external calls.
You can easily **copy and adjust it** to fit different contexts:
- Use it inside a **Business Rule**, **Script Include**, or **Flow Action Script** (see additional instructions below).
- Replace the sample `demoData` with a field value (e.g., `current.description`) to analyze the data.
- Adjust the regex to detect other patterns (emails, incident reference, etc.). See comments in the code for the additional examples.

---

### 🔧 Possible Extensions
- Parse table data (`sys_journal_field`, `kb_knowledge`) instead of static text.
- Store extracted tags in a custom table for analytics.

---

### ℹ️ Additional Instructions:
#### Use in Script Include

1. Go to **Script Includes** in the Application Navigator.
2. Click **New**, and name it (e.g., `TagExtractorUtils`).
3. Set the following options:
- **Client Callable**: `false`
- **Accessible from**: `All application scopes`
4. Paste this code:

```javascript
var TagExtractorUtils = Class.create();
TagExtractorUtils.prototype = {
initialize: function () {},

extract: function (text) {
var result = {
hashtags: text.match(/#[A-Za-z0-9_]+/g),
mentions: text.match(/@[A-Za-z0-9_]+/g)
};
return result;
},

type: 'TagExtractorUtils'
};
```
5. Use it as any other script include.

#### Use in Business Rule with a couple of custom text fields and previously created script include

1. Go to **Business Rules** in the Application Navigator.
2. Click **New**, choose a table (e.g., `sc_task`, `incident`).
3. Set **When** to `before`, `after`, or `async` (usually `async`).
4. In the **Script** section, call your tag-extracting logic:

```javascript
(function executeRule(current, previous /*null when async*/) {
var text = current.short_description + ' ' + current.description;
var tags = new TagExtractorUtils().extract(text);

current.u_hashtags = tags.hashtags.join(', ');
current.u_mentions = tags.mentions.join(', ');

})(current, previous);
```
#### Use in Flow Action Script and previously created script include

1. Go to **Flow Designer > Action** and click **New Action**.
2. Give it a name like `Extract Tags from Text`.
3. Add an **Input** (e.g., `input_text` of type String).
4. Add a **Script step**, then paste the script there:

```javascript
(function execute(inputs, outputs) {
var text = inputs.input_text || '';
var tags = new TagExtractorUtils().extract(text);

outputs.hashtags = tags.hashtags.join(', ');
outputs.mentions = tags.mentions.join(', ');
})(inputs, outputs);
```
5. Use this **Action** in your flow to extract tags by passing the text to it as a parameter.

<sub>🤖 This contribution was partially created with the help of AI.</sub>
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(function() {

var demoData = "Quick test for hashtag and mention extraction in ServiceNow. " +
"Let's make sure it catches #Hack4Good #ServiceNow #regex and mentions like @ivan and @servicenow.";

// ---------------------------------------------
// Useful regex patterns (for future extension):
// - Hashtags: /#[A-Za-z0-9_]+/g
// - Mentions: /@[A-Za-z0-9_]+/g
// - Emails: /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g
// - Ticket refs: /INC\d{7}/g (e.g., INC0012345)
// ---------------------------------------------

// Separate regex patterns for clarity
var hashtagRegex = /#[A-Za-z0-9_]+/g;
var mentionRegex = /@[A-Za-z0-9_]+/g;

// Match both types
var hashtags = demoData.match(hashtagRegex);
var mentions = demoData.match(mentionRegex);

if (hashtags.length > 0) {
gs.info('Found ' + hashtags.length + ' hashtags: ' + hashtags.join(', '));
} else {
gs.info('No hashtags found.');
}

if (mentions.length > 0) {
gs.info('Found ' + mentions.length + ' mentions: ' + mentions.join(', '));
} else {
gs.info('No mentions found.');
}

})();
Loading