Skip to content
IT Canvass
Development · Lesson

Transform map scripting

Quick answer

onBefore, onAfter, onStart, onComplete and field scripts, with the patterns that keep imports predictable.

Key takeaways

  • ignore = true skips a row cleanly
  • Field source scripts set answer, nothing else
  • Cache repeated lookups, they run per row
  • Never call an external API per row

Where scripts run

onStart runs once before the import, onBefore per row before the target write, onAfter per row after it, onComplete once at the end, and field maps can have their own source script. Choosing the right one prevents most odd behaviour.

Common patterns

Skip a row, look up a reference, and summarise at the end.

// onBefore: skip empty rows
if (!source.u_email) { ignore = true; }

// field map source script: resolve a group by name
var gr = new GlideRecord('sys_user_group');
if (gr.get('name', source.u_group_name)) { answer = gr.getUniqueValue(); }
else { answer = ''; }

// onComplete: report the outcome
gs.info('Import finished: ' + import_set.sys_id);

Keeping imports sane

Every script here runs per row, so a query inside onBefore runs thousands of times. Cache lookups in a map on the first use, avoid dot-walking in loops, and never call an external API per row. Batch those calls in onComplete instead.

Want to learn this properly?

Our live, instructor-led ServiceNow Training covers this hands-on, with real projects and a certification path.

Check your understanding

  1. Which variable skips the current row?

    • A. skip
    • B. ignore
    • C. abort
    • D. cancel
    Show answer

    B. ignore

    Setting ignore to true prevents that row being written.

  2. Where does a field source script put its result?

    • A. target
    • B. answer
    • C. current
    • D. source
    Show answer

    B. answer

    The answer variable becomes the mapped value.

Frequently asked questions

How do I stop the transform on a bad file?

Validate in onStart and set an error, so you fail fast instead of importing half a file.

Where should I write the summary email?

onComplete, once, with counts. Per row notifications turn a routine import into an inbox flood.
CallWhatsAppEnquire