Dot Walking a Target GlideRecord Reference Field
Just thought I would point out that it is possible to dot walk a reference field inside of an addQuery. There does not appear to be any depth restrictions.
Note the gs.info statement in the while loop. JavaScript has no continuation character so you can put things on lines under it. In cases like this it is a best practice while coding. It Makes the code more maintainable (better readability). White space does not impact performance noticeably. Your editor may not like it and may throw warnings. Ignore them should this happen. ![]()
Steven Bell
var incidentRecords = new GlideRecord('incident');
// Note that the comparison string is not case sensitive (as SQL syntax is not this makes sense)
incidentRecords.addQuery('location.name', 'san diego');
incidentRecords.addQuery('assigned_to.manager.name', 'bud richman');
incidentRecords.addActiveQuery();
incidentRecords.query();
gs.info('---> Total count of San Diego incident records: {0}', incidentRecords.getRowCount());
while (incidentRecords.next()) {
// Remember that gs.log is not currently scope safe.
gs.info('---> Incident {0} originated at the {1} location. Assigned to: {2}, manager: {3}',
incidentRecords.number,
incidentRecords.location.name,
incidentRecords.assigned_to.name,
incidentRecords.assigned_to.manager.name);
}
RESULTS:
*** Script: ---> Total count of San Diego incident records: 1
*** Script: ---> Incident INC0000007 originated at the San Diego location. Assigned to: David Loo, manager: Bud Richman
If you find this article helps you, don't forget to "like" it!
https://www.servicenow.com/community/developer-blog/community-code-snippets-dot-walking-a-target-gliderecord/ba-p/2271445