What It Does
The insert() method takes all field values currently set on the GlideRecord object and creates a new record in the target table. ServiceNow assigns a new sys_id, sets audit fields like sys_created_on and sys_created_by, and persists the record to the database. The method executes synchronously and blocks until the database transaction completes.
Internally, ServiceNow validates the insert operation through multiple layers. ACL checks run first to ensure the current user has create rights on the table. Required field validation follows, checking that all mandatory fields contain values. The platform then executes 'before' Business Rules, which can modify field values or abort the operation entirely by setting current.setAbortAction(true).
The method returns the sys_id as a 32-character string if successful. It returns null when the insert fails due to ACL violations, Business Rule aborts, or database constraints. Unlike some ServiceNow APIs, insert() does not throw exceptions for typical failure scenarios—it simply returns null and logs the error.
Edge cases include attempting to insert with a pre-set sys_id (which ServiceNow ignores, generating a new one), inserting into tables with complex field dependencies, and working with reference fields that point to non-existent records. The method also handles display value resolution differently than update()—reference fields must contain valid sys_ids, not display values, unless you explicitly use setDisplayValue() before inserting.
The method differs from update() in that it creates rather than modifies, and from insertMultiple() in that it handles only single records. Unlike newRecord(), which only initializes a GlideRecord for population, insert() commits data to the database immediately.
When to Use This
Use insert() when creating new records programmatically in server-side scripts. Common scenarios include generating child records from parent records, creating follow-up tasks in Business Rules, importing data from external systems, and auto-generating records based on scheduled job logic. This is the primary method for single-record creation when you need full Business Rule execution and proper audit trail generation.
Avoid insert() for bulk operations where you need to create hundreds or thousands of records—use the Import Sets API or insertMultiple() instead for better performance. Don't use it in client-side scripts (it won't work), and avoid it in loops within Business Rules where the same Business Rule might trigger recursively. For simple logging or audit records where Business Rules aren't needed, consider direct database APIs for better performance.
Common misuse includes calling insert() on a GlideRecord object that was retrieved with get() or next()—this creates duplicate records rather than updating existing ones. Always use newRecord() or a fresh GlideRecord instantiation for inserts.
Return Value
Success returns a 32-character hexadecimal string representing the new record's sys_id (e.g., '2b1e8d4f1b8f4450b89b7a4c2e4f6g8h'). This sys_id is immediately usable for queries, references, and API calls. Failure returns null (JavaScript null, not the string 'null'). The GlideRecord object itself is also updated with the new sys_id on successful insert, making subsequent operations on the same object reference the newly created record.
Always check for null return values before using the result. Common safe patterns include if (sysId) { ... } or var sysId = gr.insert(); if (!sysId) { gs.error('Insert failed'); return; }. The sys_id can be safely stored in string variables, passed to other functions, or used as reference field values without further processing.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.
Platform Behavior & Side Effects
- Executes 'before' Business Rules first, then 'after' Business Rules following the database insert, with full access to the new sys_id
- Performs ACL checks for 'create' operation on the target table, returning null if user lacks permissions rather than throwing exceptions
- Automatically populates sys_created_on, sys_created_by, sys_updated_on, sys_updated_by, and generates a unique sys_id
- Triggers notifications, workflow activities, and other record-watch mechanisms configured for the table
- Creates audit log entries if auditing is enabled for the table, recording the insert operation and field values
- Performance is moderate—faster than complex queries but slower than bulk operations; expect 10-50ms per insert depending on Business Rules
- Commits immediately in most contexts, but respects transaction boundaries when called within explicit database transactions