|
| 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 | +|  | onBefore script referencing the shared variable. | |
| 103 | +|  | onStart script defining the global variable (`this.managerMap`). | |
| 104 | + |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +``` |
| 109 | +
|
| 110 | +``` |
| 111 | + |
0 commit comments