Skip to content

Commit bd37e54

Browse files
authored
Add Global Variable in Transform Map...
... snippet with README and screenshots
1 parent 9215b5e commit bd37e54

5 files changed

Lines changed: 97 additions & 0 deletions

File tree

86.4 KB
Loading
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```markdown
2+
### Global Variable in Transform Map
3+
4+
Demonstrates how to define and reuse a global variable (e.g., `this.managerMap`) across Transform Map scripts in ServiceNow.
5+
By using `this` instead of `var`, the variable persists during the transform execution — enabling shared lookup data and improving performance.
6+
Includes example onStart/onBefore scripts and screenshots.
7+
```
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Global Manager Map in Transform Script
2+
3+
## Overview
4+
5+
This snippet demonstrates how to build and reuse a **global lookup map** across Transform Map scripts in ServiceNow — specifically, mapping **Manager Email → sys_id** from the `sys_user` table.
6+
7+
By defining the map using `this.managerMap` in an **onStart Transform Script**, the data becomes available to **onBefore** or **onAfter** scripts during the same import execution, eliminating repetitive queries and improving performance.
8+
9+
> **DISCLAIMER**
10+
> This script was developed and tested on a **ServiceNow Personal Developer Instance (PDI)**.
11+
> It is intended for **educational and demonstration purposes only**.
12+
> Please **test thoroughly in a non-production environment** before deploying to production.
13+
14+
---
15+
16+
## What it does
17+
18+
- Preloads a dictionary of all system users (`sys_user` table) into memory.
19+
- Maps user **email addresses** (lowercased) to their corresponding **sys_ids**.
20+
- Shares the map globally across transform script stages (onStart → onBefore → onAfter).
21+
- Eliminates the need for repeated `GlideRecord` queries in each transform stage on each row.
22+
- Significantly improves transform performance when importing large data sets.
23+
24+
---
25+
26+
## Prerequisites & Dependencies
27+
28+
Before using this snippet, ensure that:
29+
30+
1. **Transform Map Context**
31+
32+
- The script must be placed in a **Transform Map > Script** (e.g., “onStart” or “Run Script”).
33+
- Other transform scripts in the same map (e.g., onBefore or transformRow) can then reference `this.managerMap`.
34+
35+
2. **sys_user Table Access**
36+
37+
- The transform user must have permission to **read** the `sys_user` table.
38+
39+
3. **Valid Email Data**
40+
- Ensure the `sys_user.email` field is populated and unique for all users who may appear as managers.
41+
42+
---
43+
44+
## Script Details
45+
46+
- **Author:** Anasuya Rampalli ([anurampalli](https://github.com/anurampalli))
47+
- **Version:** 1.0
48+
- **Date:** 2025-10-10
49+
- **Context:** Transform Map → Run Script (onStart)
50+
- **Tested On:** ServiceNow Personal Developer Instance (PDI)
51+
52+
---
53+
54+
```
55+
56+
```
140 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Script: Manager Email → Sys_id Map
3+
* Type: Transform Map Script (Run Script)
4+
*
5+
* Purpose:
6+
* This transform script builds an in-memory dictionary (map) that links
7+
* manager email addresses to their corresponding sys_ids in the `sys_user` table.
8+
*
9+
* Why:
10+
* During a data import or transform, multiple records may need to look up
11+
* the same manager repeatedly. Instead of running a GlideRecord query every time,
12+
* this preloads all manager sys_ids once for efficient lookup.
13+
*
14+
* Key Concept:
15+
* - `this.managerMap` is used instead of `var managerMap` so that the map
16+
* persists across multiple transform runs (e.g., onBefore/transformRow/onAfter)
17+
* within the same Transform Map execution context.
18+
* - Variables defined with `this` become properties of the transform script's
19+
* execution object, allowing reuse across all functions available with the specific transform map pbject.
20+
*
21+
* Example usage in an onBefore/transformRow script:
22+
* var managerSysId = this.managerMap[source.manager_email.toLowerCase()];
23+
*/
24+
25+
(function runTransformScript(source, map, log, target /*undefined onStart*/) {
26+
// Build dictionary of Manager Emails → Sys_id
27+
this.managerMap = {};
28+
var grManagerUser = new GlideRecord("sys_user");
29+
grManagerUser.query();
30+
while (grManagerUser.next()) {
31+
managerMap[grManagerUser.email.toString().toLowerCase()] =
32+
grManagerUser.sys_id.toString();
33+
}
34+
})(source, map, log, target);

0 commit comments

Comments
 (0)