|
| 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> |
0 commit comments