Skip to content

Commit d2f5d82

Browse files
authored
Add Global Variable in Transform Map... (#1994)
* Add Global Variable in Transform Map... ... snippet with README and screenshots * Delete Server-Side Components/Transform Map Scripts/Global Variable in Transform Map/README-title.md Deleting unnecessary file * Update README.md Updated with usage example (onBefore script) and screenshots * added the onBefore script as a .js file
1 parent 098fd89 commit d2f5d82

File tree

5 files changed

+181
-0
lines changed

5 files changed

+181
-0
lines changed
86.4 KB
Loading
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
## Example Scripts
55+
56+
### onBefore Script — Use Global Variable
57+
58+
```javascript
59+
(function runTransformScript(source, map, log, target /*undefined onStart*/ ) {
60+
61+
// Name split
62+
if (source.u_full_name) {
63+
var parts = source.u_full_name.trim().split(/\s+/);
64+
target.first_name = parts[0];
65+
if (parts.length > 1) {
66+
target.last_name = parts.slice(1).join(" ");
67+
}
68+
}
69+
70+
// Email normalize
71+
if (source.u_email) {
72+
target.email = source.u_email.toString().toLowerCase();
73+
}
74+
75+
// Manager mapping
76+
var managerMap = this.managerMap;
77+
gs.info('manager map: ' + managerMap);
78+
if (source.u_manager_email && managerMap) {
79+
80+
var managerEmail = source.u_manager_email.toString().toLowerCase();
81+
var managerSysId = managerMap[managerEmail];
82+
gs.info('TM HR User Dump managerSysId: ' + managerSysId);
83+
84+
if (managerSysId) {
85+
target.manager = managerSysId;
86+
} else {
87+
log.warn("Manager email not found: " + managerEmail + " for user " + source.u_full_name);
88+
target.manager = ""; // optional: blank manager instead of error
89+
}
90+
}
91+
92+
93+
})(source, map, log, target);
94+
```
95+
96+
---
97+
98+
## Screenshots
99+
100+
| Screenshot | Description |
101+
| --------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------- |
102+
| ![UsingGlobalManagerMapInOnBeforeScriptScreenshot](./UsingGlobalManagerMapInOnBeforeScriptScreenshot.png) | onBefore script referencing the shared variable. |
103+
| ![OnstartScriptScreenshot](./OnstartScriptScreenshot.png) | onStart script defining the global variable (`this.managerMap`). |
104+
105+
106+
---
107+
108+
```
109+
110+
```
111+
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);
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
(function runTransformScript(source, map, log, target /*undefined onStart*/) {
2+
// Name split
3+
if (source.u_full_name) {
4+
var parts = source.u_full_name.trim().split(/\s+/);
5+
target.first_name = parts[0];
6+
if (parts.length > 1) {
7+
target.last_name = parts.slice(1).join(" ");
8+
}
9+
}
10+
11+
// Email normalize
12+
if (source.u_email) {
13+
target.email = source.u_email.toString().toLowerCase();
14+
}
15+
16+
// Manager mapping
17+
var managerMap = this.managerMap;
18+
gs.info("manager map: " + managerMap);
19+
if (source.u_manager_email && managerMap) {
20+
var managerEmail = source.u_manager_email.toString().toLowerCase();
21+
var managerSysId = managerMap[managerEmail];
22+
gs.info("TM HR User Dump managerSysId: " + managerSysId);
23+
24+
if (managerSysId) {
25+
target.manager = managerSysId;
26+
} else {
27+
log.warn(
28+
"Manager email not found: " +
29+
managerEmail +
30+
" for user " +
31+
source.u_full_name
32+
);
33+
target.manager = ""; // optional: blank manager instead of error
34+
}
35+
}
36+
})(source, map, log, target);

0 commit comments

Comments
 (0)