Development · LessonBy Praveen T, ServiceNow Trainer, 9 yrs · Published · ServiceNow · all levels
Transform map scripting
onBefore, onAfter, onStart, onComplete and field scripts, with the patterns that keep imports predictable.
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.
Practice challenge
+0 XPStreak ×0
Question 1 of 2
Which variable skips the current row?
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.