The insert() method returns the sys_id of the newly created record as a string — but only if the insert succeeds. Most developers miss the critical failure case: if the insert fails due to ACLs, required field validation, or business rule exceptions, insert() returns null or an empty string. This pattern is essential when you need to create related records in the same script execution — using the returned sys_id eliminates the need for additional queries to find the record you just created.

When to use this

  • Creating parent-child relationships in the same script (incidents with tasks, requests with catalog items)
  • Building audit trails or related records that reference the primary record's sys_id
  • Logging or notification scripts that need to reference the specific record created
  • Integration scripts that need to store the ServiceNow sys_id in an external system

When NOT to use this

  • Don't use this in client-side scripts — GlideRecord.insert() doesn't return the sys_id on the client
  • Don't use this for bulk record creation — use insertMultiple() or batch REST API calls instead
  • Don't assume the insert succeeded — always check if the returned value is truthy before using it
  • Don't call insert() inside a loop over another GlideRecord — you'll create N+1 database operations

Key behaviors and gotchas

  • The returned sys_id is a 32-character string, not a GlideRecord object — use it directly in reference fields
  • Business rules on the target table will fire during insert() — if they throw exceptions, insert() returns null
  • ACL failures will cause insert() to return null — the record is not created and no error is thrown
  • Required field validation happens during insert() — missing mandatory fields will cause the insert to fail silently
  • The sys_id is generated before business rules fire — you can access it in 'before' business rules via current.sys_id
  • Domain separation applies — you can only insert records into domains you have access to
⚠️

Never assume insert() succeeded. A null return value means the record wasn't created, but your script continues running. Always check the return value before using it to avoid creating orphaned related records or broken references.

Free Newsletter

Enjoying this? Get one deep-dive per week.

Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.

No spam · Unsubscribe anytime