Reference fields will handle this for you.
If you use ready-made tables in ServiceNow for the user ( sys_user ) and company ( core_company ), they are linked by the user reference field ( sys_user.company ).
Using the help fields (essentially foreign keys), you can use dot-walk to query through the reference field to query the fields in the specified record. A GlideRecord query to retrieve all users in a London based company would look like this:
var user = new GlideRecord('sys_user'); user.addQuery('company.city', 'London'); user.query(); while (user.next()) { gs.info("User: " + user.user_name); gs.info("Company: " + user.company.name); gs.info("Company Address: " + user.company.street); gs.info("Company City: " + user.company.city); }
You can do the same with the request via encoded URLs:
yourinstance.service-now.com/sys_user_list.do?sysparm_query=company.city=London
Either the GlideRecord request or the encoded URL ends up producing SQL under the hood that performs the search you are looking for (you can activate SQL Debug SQL debugging as an administrator to see the generated sql):
SELECT ... FROM sys_user LEFT JOIN core_company ON sys_user.company = core_company.sys_id WHERE core_company.city = 'London'
Now you actually cannot use these OOB tables, but the relationships you are trying to query against using joins will be resolved with similarly configured reference fields
source share