ホーム 暮らし Transformation Scripts: Mechanisms for Intercepting the Import Process

Transformation Scripts: Mechanisms for Intercepting the Import Process

八木 麻衣 八木 麻衣 • 更新: • 公開: • 読了約 47 分
目次


This is the sixth installment in the import and integration series. Up to the previous article, we looked at the framework of imports, including data sources, transformation maps, coalesce, and schedules. This time, we will cover the mechanism for intercepting the process, known as Transformation Scripts (sys_transform_script).

In the article on transformation maps (part 2), I mentioned that the map itself also has a Script field. Apart from that, there are scripts organized by event, which are designed to "run at this specific timing of the transformation." You can insert custom processing before or after the import, or before or after the transformation of each row. We will decipher this mechanism using 12 examples from an actual instance.

The overall picture of imports and integration is summarized in ServiceNow Data Integration Overview.

What are Transformation Scripts: Intercepting by Timing

n7eca23eff887

Transformation scripts are scripts that run automatically at fixed timings during the transformation process. You can specify when they run, such as "when the import starts," "immediately before transforming each row," or "when all transformations are complete."

While the Script field in the map itself is for "creating a value for a single field," transformation scripts are for "intercepting the transformation flow to insert custom processing." For example, you can write logic to skip rows that do not meet conditions or to update another table after transformation. The role of a transformation script is to insert your own processing at key points while keeping the main flow of the import intact.

Think of it like an inspector standing on a factory production line. The flow of the line (transformation) continues to move, and you place inspectors (scripts) at fixed positions. The inspector checks the items as they pass by, rejecting defective products or marking them. The inspector's role changes depending on where they are stationed (when). If you place them at the start, they are preparation staff; if you place them before each item, they are gatekeepers; if you place them at the end, they are tally clerks. It is easier to grasp the overall picture if you view transformation scripts as a mechanism for deciding "where on the line to place what kind of inspector."

when: The 5 timings for interception

You decide when to intercept using a field called when. The following 5 types of timings were confirmed on the actual instance.


  • onStart ... At the start of the transformation (once for the entire process)

  • onBefore ... Immediately before transforming each row (runs for every row)

  • onAfter ... Immediately after transforming each row (runs for every row)

  • onComplete ... At the completion of the transformation (once for the entire process)

  • onReject ... When a row is rejected

onStart and onComplete run "once for the entire import," while onBefore and onAfter run "once for every row." Therefore, you use onStart/onComplete for overall pre-processing and post-processing, and onBefore/onAfter for detailed decisions on a per-row basis.


If you visualize this, you can see "where" each timing fires in the import flow. onStart at the beginning, onBefore → (transformation) → onAfter within the loop for each row, and onComplete after everything is finished. If you set ignore in onBefore, that row will be skipped without being transformed. The 5 timings are simply lined up along this single flow.

Let's also look at the distribution of actual measurements.


When classifying the 12 items by 'when', 'onBefore' was the overwhelming majority with 8 items. The others were 2 for 'onStart', 1 for 'onAfter', 1 for 'onComplete', and 0 for 'onReject'. 'onBefore', which intervenes with a decision for every line, is the main player. Most situations where you want to write custom processing during an import involve line-by-line decisions such as 'should I include this line, or how should I change it?'. This distribution makes that clear.

Opening onBefore: Skipping a line

Let's look at an actual example of 'onBefore', which is the most common. This is a verification script for OIDC integration.


The 'When' is 'onBefore', and 'Active' is on. The code inside looked like this.

(function runTransformScript(source, map, log, target) 
    var oidcTransformCheck = new OIDCTransformCheck(source, map, log, target);
    var isMappingValid = oidcTransformCheck.validateMappingOnBefore();
    if(!isMappingValid) 
        ignore = true;
    
)(source, map, log, target);

Reading it is straightforward. It verifies whether the OIDC mapping is correct, and if it is not, it sets 'ignore = true'. This 'ignore = true' is the key, and setting this skips the import of that line. In other words, it acts as a gatekeeper that 'skips invalid data lines without putting them into production'. You inspect the contents of the line in 'onBefore' and reject lines that do not meet the conditions here. This is the typical way to use 'onBefore'.

The screen also displayed a note saying, 'The functionality of this script and the globals you can use change depending on when it is executed (when)'. I want to keep in mind that even with the same way of writing, the tools available change depending on the 'when'.

The reason 'onBefore' is excellent as a gatekeeper is that it can make decisions 'before entering production'. If you notice strange data in 'onAfter' (after conversion), a production record has already been created by that time. It takes extra effort to cancel it. With 'onBefore', you can reject problematic lines 'before they are entered', so production does not get dirty. Therefore, if you want to protect the quality of the data being imported, the standard practice is to inspect it with 'onBefore' first. The reason the OIDC verification script is placed in 'onBefore' was also to 'stop it before it enters'. It is the idea of placing a gatekeeper at the entrance of the data.

Four arguments: source, target, map, and log

On the first line of the code, it says 'runTransformScript(source, map, log, target)'. These four are the tools you can use in a transformation script.

  • source ... Staging line (raw data imported). Read it like 'source.email'

  • target ... The production record you are trying to create. Write it like 'target.user_name = ...'

  • map ... The transformation map currently being applied

  • log ... Object for log output

The 'Script' in the map body seen in (2) also used the same 'source' and 'target'. You read the staging values and write to the production record. You can touch the values during the transformation using these two variables as the axis. However, there is a point to note: when it is 'onStart', 'source' and 'target' do not exist yet (the code comment also said 'undefined onStart'). This is because 'onStart' is 'before the processing of the line begins', so no lines have been handled yet. This is what it means when it says the tools you can use change depending on the 'when'.

Specifically, even if you try to read 'source.email' in 'onStart', it will result in an error because 'source' does not exist. In 'onStart', you do not touch the lines yet and only perform overall preparations. On the other hand, in 'onBefore' or 'onAfter', both 'source' and 'target' are available, so you can freely read and write line values. Therefore, the division of roles is to write 'processing that uses line data' in 'onBefore'/'onAfter', and 'overall preparation/closing that is unrelated to lines' in 'onStart'/'onComplete'. Because the tools you can use change with 'when', you will not make a mistake if you think in the order of choosing the 'when' according to the content of the processing you are writing. The reason the note on the screen emphasizes that 'the globals you can use change with when' is to help you avoid this pitfall.

Where to use onAfter, onComplete, and onReject

Let's also cover the remaining timings. onAfter is immediately after the conversion of each line is finished. Since 'target' (the production record) has been created, it is suitable for 'using the created record to perform further processing'. For example, post-processing such as creating another record linked to the created user.


The onAfter script on the actual machine also followed the same runTransformScript(source, map, log, target) format. The only difference is that the When field is set to onAfter. The framework for writing them is common; by simply changing when it runs, you change the timing of the interruption. Thanks to this unified design, once you learn one way of writing them, you can write scripts for any timing in the same manner.

onComplete runs only once after the entire transformation is finished. It is suitable for closing processes such as "summarizing how many items were processed in this import" or "notifying the person in charge of completion." onReject is a framework that fires when a row is rejected. Although there were zero cases in this environment, it serves as a window for error handling, such as "recording why it was rejected."

You prepare with onStart, act as a gatekeeper with onBefore, and perform post-processing with onAfter. You wrap up with onComplete and handle errors with onReject. If you line up these five timings along the flow of the import, you can see where you should write what.

Differences from the Map's own Script

Here, we will compare the differences between the map's own Script seen in (2) and this transform script.

The map's own Script was for creating field values. For a User map, it is used to generate values, such as constructing a user_name from a full name. On the other hand, this transform script is for intercepting the transformation flow. It is responsible for controlling processes such as skipping rows or inserting post-processing.

Even though both are "scripts," one creates values and the other intercepts processes. The places where you write them are also different. Value generation is in the Script field of the map, while interception is a separate record called sys_transform_script. Which one you should use is decided by whether you "want to create a value" or "want to insert a process." If you mistake this usage, you will struggle because it won't run at the intended timing, so this is a distinction you want to grasp from the start.


Looking at the table definition of sys_transform_script, several columns are lined up. These include when, script, map, order, and active. What is surprising here is that there is no name column. Transform scripts are identified by "which map and which when" and do not have names. Therefore, in the list, you distinguish them by map and when, not by name. This point can be confusing if you try to search by name, so it is useful to remember it as a point of caution gained from actual measurement. Once you understand the column structure, you won't get lost when handling transform scripts from REST or scripts. You can also attach multiple when scripts to the same map. When you want to see "the onBefore and onAfter of this map" side by side, it is convenient to filter by map and then sort by when.

To avoid writing in the wrong place

I will summarize the locations of scripts related to transformation here. As seen in (2) and this session, there are three places within a transformation where you can write scripts.

  1. Map's own Script (sys_transform_map.script) ... Generates field values on the spot

  2. Transform Script (sys_transform_script) ... Intercepts each timing of the transformation (this session)

  3. Field Map's Source script (sys_transform_entry) ... A small script that creates a value for one item

Even though they are all "transformation scripts," these three have different running timings and scopes. For processing just one item, use the Field Map's Source script; for generating values related to the entire row or the entire map, use the map's own Script; and for intercepting the processing flow, use the transform script. If you think about "at what granularity" and "when" the process you want to write should run, you can decide which of the three you should write in. Having this map will help you avoid stumbling blocks like "I wrote it but it doesn't run" or "it runs at an unintended timing." If you are troubled by import scripts, the shortcut is to first distinguish which of these three it is about.

Seeing the whole picture in a list

Finally, let's return to the list of transform scripts.


There are 12 items sorted by when. You can also see which transformation map each is linked to. The OIDC-based maps (Okta / Google / ADFS / Auth0 / Azure) all had the same form of onBefore validation script. It is a common design where user data imported from an external IdP is checked for mapping validity row by row before being inserted. The pattern of placing a gatekeeper on each map can also be read from this list.

The fact that five maps have the same process is, conversely, proof that "this validation is standard for external IdP integration." Although the data format differs slightly for each IdP, the defensive approach of "checking the mapping before importing and rejecting it if it is strange" is common. That is why ServiceNow placed the same form of gatekeeper on each IdP map. If you are creating a new import in your own company, this OOTB onBefore script is a good model. This is because the way of rejecting rows with ignore = true can be applied as is. Standard records on an actual machine are both an explanation of functionality and a template for when you write your own. Reading such a list allows you to understand both.

Confirmation Path

These are the entry points for opening on a live instance (URL formats are based on list.do conventions. Actual click verification is outside the scope of this document).

  • List of conversion scripts … sys_transform_script_list.do (sorted by the 'when' column)

  • Single conversion script … sys_transform_script.do?sys_id=...

  • Related list 'Transform Scripts' on the transform map

Summary

Conversion scripts are a mechanism to 'intervene at specific timings' during the import conversion process. You choose from five options for 'when': onStart, onBefore, onAfter, onComplete, or onReject. You write the processing logic using four tools: source, target, map, and log. In the 12 examples on the live instance, 'onBefore' was the primary focus, and skipping rows using 'ignore = true' was a typical use case.

If the script in the map itself is for 'creating values,' then conversion scripts are for 'intervening in the process.' By remembering the difference in roles and where to write them, you can confidently know where to make changes when you need to write custom logic for an import.

Looking back, import conversion is not just a simple task of mechanically moving columns. You map them, prevent duplicates with coalesce, create values with scripts, and then insert judgments at key points with conversion scripts. Multiple layers of mechanisms overlap to transform messy external data into clean data ready for production use. Conversion scripts are the components responsible for those final, fine-tuned adjustments. They aren't flashy, but they absorb site-specific requirements like 'don't import this row' or 'do this after the import.' They allow you to insert that extra bit of effort that standard features alone cannot provide, at exactly the right time. It is precisely because this room for customization exists that ServiceNow imports can adapt to diverse environments.

Next time, we will look at logs and error rows that record the results of an import. This will cover where to check whether things went well or where they failed.

Other records verified on the live instance

Terminology
  • Conversion script (sys_transform_script) … A script that intervenes at a specific timing of the conversion

  • when … The timing to intervene (onStart / onBefore / onAfter / onComplete / onReject)

  • ignore = true … If set in onBefore, it skips the import of that row

  • source / target … Staging row / The production record being created

  • Difference from the script in the map itself … Value generation (map) vs. intervention in processing (conversion script)

URLs for checking in your own environment

You can open the same screens by prepending your instance name.

  • /sys_transform_script_list.do?sysparm_query=ORDERBYwhen (12 conversion scripts)

  • /sys_transform_script.do?sys_id=... (when and code for 1 script)

  • "Transform Scripts" related list on the Transform Map

Key Points of Actual Measurement (Reiterated)
  • 12 sys_transform_script records. Breakdown by when: onBefore 8 / onStart 2 / onAfter 1 / onComplete 1 / onReject 0

  • Signature: runTransformScript(source, map, log, target)

  • Typical onBefore: Validate and skip if invalid by setting ignore = true (OIDC-related)

  • In onStart, source / target are undefined (because it is before row processing)

Scope of Verification and Disclaimer

  • The counts, 'when' breakdown, script content, and signatures in this article are based on actual measurements from PDI (Zurich / OOTB).

  • The actual execution of the transformation and the triggering of the scripts are unconfirmed (outside the scope of verification) because there are 0 sys_import_set records.

  • OIDC validation scripts are standard included records and are generally not rewritten on actual machines.

  • The list.do URL format is based on conventions, and actual click verification is outside the scope of this article.

Reference Links

  • Transform maps (ServiceNow Docs)

  • Import sets (Understanding Import Sets - ServiceNow Community)

  • Administration: Importing & Transforming External Data (ServiceNow Community)

  • Import Set Coalesce (ServiceNow Docs)

  • Import/Transform Learning Path (ServiceNow Community)

  • Getting Started with Import Sets (ServiceNow Developer Blog)

  • What Is Coalesce in ServiceNow (ServiceNow Community)

  • Importing Data using Transform Map (ServiceNow Community)

同じカテゴリの記事

八木 麻衣
トレンドリサーチャー 八木 麻衣
プロフィールを見る →

全記事の事実確認を担当。一次情報・公式発表・公開資料の三点を確認できない場合は公開を見送る判断をします。

おすすめの関連記事