Tushar
Kilo Sage
Options
- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
Wednesday
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
- Reference fields must be valid – Use field_name.display_field (e.g., user_name, name) to match records.
- It creates records if they don’t exist – Great for automation, dangerous in prod without validation.
- Use in Business Rules? – Only in after rules with current.updateWithReferences().
- Performance – Faster than multiple queries, but still does DB operations per reference.
- Debugging – Wrap in try/catch and use gs.error(inc.getLastErrorMessage()) if it fails.
- 135 Views
Comments
Niamul Arifin
Tera Expert
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Inappropriate Content
Wednesday
I am asking the same question to myself. Why these two methods came across after working on ServiceNow ecosystem for so long? Great finding
