logo

NJP

One GlideRecord, Many Updates: Mastering insertWithReferences() & updateWithReferences()

New article articles in ServiceNow Community · Oct 29, 2025 · article

I’ve been working with ServiceNow for years, and every now and then I stumble on a gem that makes me wonder: “Why didn’t I use this sooner?”

Today, I want to share two of my favorite GlideRecord methods that save time, reduce code, and make integrations way cleaner:

With insertWithReferences() and updateWithReferences(), you build the entire object graph in memory, and ServiceNow handles the rest.

// 1. INSERT with references – creates incident + updates/creates caller + location var inc = new GlideRecord('incident'); inc.initialize(); inc.short_description = "Fresh incident via insertWithReferences"; inc.caller_id.user_name = "Test.User"; inc.caller_id.first_name = "Test"; inc.caller_id.last_name = "User"; inc.caller_id.email = "test.user@example.com"; inc.caller_id.location.name = "Noida Office"; inc.caller_id.location.city = "Noida"; inc.caller_id.location.state = "Uttar Pradesh"; inc.caller_id.location.country = "India"; inc.caller_id.location.zip = "201309"; var newIncSysId = inc.insertWithReferences(); gs.info("Created incident: " + newIncSysId);

  • ServiceNow sees caller_id → looks up or creates the user
  • Sees location → looks up or creates the location record
  • Links everything correctly
  • Returns the incident sys_id var current = new GlideRecord('incident'); if (current.get(newIncSysId)) { current.short_description = "Updated via updateWithReferences"; current.caller_id.email = "test.user01@example.com"; current.caller_id.location.zip = "201310"; current.updateWithReferences(); gs.info("Incident and references updated!"); }

No need to:

  • Query the user separately
  • Query the location
  • Handle setValue() vs setDisplayValue()
  • Worry about reference mismatches

Pro Tips

  1. Reference fields must be valid – Use field_name.display_field (e.g., user_name, name) to match records.
  2. It creates records if they don’t exist – Great for automation, dangerous in prod without validation.
  3. Use in Business Rules? – Only in after rules with current.updateWithReferences().
  4. Performance – Faster than multiple queries, but still does DB operations per reference.
  5. Debugging – Wrap in try/catch and use gs.error(inc.getLastErrorMessage()) if it fails.
View original source

https://www.servicenow.com/community/developer-articles/one-gliderecord-many-updates-mastering-insertwithreferences-amp/ta-p/3415181