Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Tushar
Kilo Sage
Kilo Sage

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.

 

 

Comments
Niamul Arifin
Tera Expert

I am asking the same question to myself. Why these two methods came across after working on ServiceNow ecosystem for so long? Great finding 

Version history
Last update:
3 weeks ago
Updated by:
Contributors