Read incidents using reference fields
Demonstrates how to query the incident table using
reference (foreign key) fields in ServiceNow.
ServiceNow allows filtering on fields from related tables using
dot-walking syntax (reference_field.some_field),
without requiring explicit joins.
Use these patterns to filter incidents by Caller, Assigned User, Assignment Group, Configuration Item, or other referenced records.
Standard SQL query example
This is the base query accepted by the connector. To execute it in SQL Server, you have to pass it to the Data Gateway via a Linked Server. See how to accomplish this using the examples below.
-- 1. Query incidents by caller's username (caller_id → sys_user)
SELECT *
FROM get_table_rows
WITH(
TableName='incident',
Query='caller_id.user_name=john.doe'
)
-- 2. Query incidents by assigned user's display name (assigned_to → sys_user)
SELECT *
FROM get_table_rows
WITH(
TableName='incident',
Query='assigned_to.name=Jane Smith'
)
-- 3. Query incidents by assignment group name (assignment_group → sys_user_group)
SELECT *
FROM get_table_rows
WITH(
TableName='incident',
Query='assignment_group.name=Network'
)
-- 4. Query incidents by configuration item name (cmdb_ci → CMDB record)
SELECT *
FROM get_table_rows
WITH(
TableName='incident',
Query='cmdb_ci.name=SRV-APP-01'
)
-- 5. Combine reference field and local field filters
-- (Network group + High priority incidents)
SELECT *
FROM get_table_rows
WITH(
TableName='incident',
Query='assignment_group.name=Network^priority=1'
)
Using OPENQUERY in SQL Server
SELECT * FROM OPENQUERY([LS_TO_SERVICENOW_IN_GATEWAY], '-- 1. Query incidents by caller''s username (caller_id → sys_user)
SELECT *
FROM get_table_rows
WITH(
TableName=''incident'',
Query=''caller_id.user_name=john.doe''
)
-- 2. Query incidents by assigned user''s display name (assigned_to → sys_user)
SELECT *
FROM get_table_rows
WITH(
TableName=''incident'',
Query=''assigned_to.name=Jane Smith''
)
-- 3. Query incidents by assignment group name (assignment_group → sys_user_group)
SELECT *
FROM get_table_rows
WITH(
TableName=''incident'',
Query=''assignment_group.name=Network''
)
-- 4. Query incidents by configuration item name (cmdb_ci → CMDB record)
SELECT *
FROM get_table_rows
WITH(
TableName=''incident'',
Query=''cmdb_ci.name=SRV-APP-01''
)
-- 5. Combine reference field and local field filters
-- (Network group + High priority incidents)
SELECT *
FROM get_table_rows
WITH(
TableName=''incident'',
Query=''assignment_group.name=Network^priority=1''
)')
Using EXEC in SQL Server (handling larger SQL text)
The major drawback of OPENQUERY is its inability to incorporate variables within SQL statements.
This often leads to the use of cumbersome dynamic SQL (with numerous ticks and escape characters).
Fortunately, starting with SQL 2005 and onwards, you can utilize the EXEC (your_sql) AT [LS_TO_SERVICENOW_IN_GATEWAY] syntax.
DECLARE @MyQuery NVARCHAR(MAX) = '-- 1. Query incidents by caller''s username (caller_id → sys_user)
SELECT *
FROM get_table_rows
WITH(
TableName=''incident'',
Query=''caller_id.user_name=john.doe''
)
-- 2. Query incidents by assigned user''s display name (assigned_to → sys_user)
SELECT *
FROM get_table_rows
WITH(
TableName=''incident'',
Query=''assigned_to.name=Jane Smith''
)
-- 3. Query incidents by assignment group name (assignment_group → sys_user_group)
SELECT *
FROM get_table_rows
WITH(
TableName=''incident'',
Query=''assignment_group.name=Network''
)
-- 4. Query incidents by configuration item name (cmdb_ci → CMDB record)
SELECT *
FROM get_table_rows
WITH(
TableName=''incident'',
Query=''cmdb_ci.name=SRV-APP-01''
)
-- 5. Combine reference field and local field filters
-- (Network group + High priority incidents)
SELECT *
FROM get_table_rows
WITH(
TableName=''incident'',
Query=''assignment_group.name=Network^priority=1''
)'
EXEC (@MyQuery) AT [LS_TO_SERVICENOW_IN_GATEWAY]