Get a list of incidents [Read more...]
SELECT * FROM incident
--Using Primary Key
--SELECT * FROM any_table_here WHERE sys_id='109562a3c611227500a7b7ff98cc0dc7' --Primary Key in WHERE clause
--Using Filter Expression
--SELECT * FROM incident WITH(Query='number=INC0000001') --Equal condition
--SELECT * FROM incident WITH(Query='number!=INC0000001') --Not equal condition
--SELECT * FROM incident WITH(Query='numberININC0000001,INC0000002,INC0000003') --IN condition
--SELECT * FROM incident WITH(Query='number=INC0000001^state=7') --AND condition
--SELECT * FROM incident WITH(Query='number=INC0000001^ORnumber=INC0000002') --OR condition
--SELECT * FROM incident WITH(Query='numberLIKE0001') --LIKE condition
--SELECT * FROM incident WITH(Query='numberSTARTSWITHINC00') --StartWith condition
--SELECT * FROM incident WITH(Query='numberENDSWITH0001') --StartWith condition
--SELECT * FROM incident WITH(Query='number=INC0000001^state=7^NQORnumber=INC0000002') --AND / OR MIXED using NQ (NewQuery Operator) -- (number=INC0000001 and state=7) OR (number=INC0000002)
--more information about filter here https://docs.servicenow.com/bundle/utah-platform-user-interface/page/use/common-ui-elements/reference/r_OpAvailableFiltersQueries.html
-- To read all available tables execute this query:
-- SELECT * FROM Tables
-- Other common tables:
-----------------------
-- SELECT * FROM sys_db_object
-- SELECT * FROM sys_dictionary
-- SELECT * FROM sys_user
-- SELECT * FROM sys_user_has_role
-- SELECT * FROM sys_user_grmember
-- SELECT * FROM task
-- SELECT * FROM task_sla
-- SELECT * FROM incident
-- SELECT * FROM incident_sla
-- SELECT * FROM change_request
-- SELECT * FROM cmdb_ci_computer
-- SELECT * FROM cmdb_ci_outage
-- SELECT * FROM cmdb_ci
-- SELECT * FROM sn_customerservice_case
-- SELECT * FROM kb_knowledge
-- SELECT * FROM kb_use
-- SELECT * FROM sc_req_item
-- SELECT * FROM sc_request
-- SELECT * FROM sc_task
Get an incident by sys_id [Read more...]
SELECT * FROM incident
WHERE sys_id = 'SYS_ID_GOES_HERE'
Get only selected columns and speed up the query [Read more...]
By default all column values are returned in the result set. However, you can instruct the ServiceNow API to send only specific columns you care about by setting 'Fields' parameter in the WITH clause. This will speed up the response significantly.
SELECT number,name FROM incident WITH (Fields='number,name') --Setting Fields parameter will speed up the query
Get the display value of a reference field [Read more...]
This example shows how to get related record(s) from the reference field. Basically, you need to know referenced table name for that referenced field. Then you can query by sys_id (Primary Key)
--STEP#1 returns incident number and related company (sys_id)
SELECT number,company FROM incident
--STEP#2 find related table for company field
select name,reference from get_table_columns order by 1 WITH(TableName='incident')
--STEP#3 query related table with sys_id returned in step#1
select sys_id, name from core_company where sys_id='31bea3d53790200044e0bfc8bcbe5dec'
Query table using serverside filter expression [Read more...]
This example shows how to filter data from a table using server side filter expression. Its always faster to do this way rather than using in WHERE clause (Client Side Filter)
SELECT * FROM incident WITH(Query='number=INC0000001') --Equal condition
--syntax (single condition)
--sysparm_query=<col_name><operator><value>
--syntax (multiple conditions)
--sysparm_query=<col_name><operator><value>[<operator><col_name><operator><value>]
--more info check below links
-- https://docs.servicenow.com/bundle/rome-application-development/page/integrate/inbound-rest/concept/c_TableAPI.html#title_table-GET
-- https://docs.servicenow.com/bundle/utah-platform-user-interface/page/use/common-ui-elements/reference/r_OpAvailableFiltersQueries.html
--More examples
--SELECT * FROM any_table_here WHERE sys_id='109562a3c611227500a7b7ff98cc0dc7' --Primary Key in WHERE clause
--SELECT * FROM incident WITH(Query='number=INC0000001') --Equal condition
--SELECT * FROM incident WITH(Query='number!=INC0000001') --Not equal condition
--SELECT * FROM incident WITH(Query='numberININC0000001,INC0000002,INC0000003') --IN condition
--SELECT * FROM incident WITH(Query='number=INC0000001^state=7') --AND condition
--SELECT * FROM incident WITH(Query='number=INC0000001^ORnumber=INC0000002') --OR condition
--SELECT * FROM incident WITH(Query='numberLIKE0001') --LIKE condition
--SELECT * FROM incident WITH(Query='numberSTARTSWITHINC00') --StartWith condition
--SELECT * FROM incident WITH(Query='numberENDSWITH0001') --StartWith condition
--SELECT * FROM incident WITH(Query='number=INC0000001^state=7^NQORnumber=INC0000002') --AND / OR MIXED using NQ (NewQuery Operator) -- (number=INC0000001 and state=7) OR (number=INC0000002)
INSERT INTO incident(incident_state, severity, category, cause)
VALUES (3, 1, 'software', 'Missing a software feature.')
UPDATE incident
SET cause = 'A bug in the software'
,severity = 3
,incident_state = 2
WHERE sys_id = 'SYS_ID_GOES_HERE'
DELETE incident
WHERE sys_id = 'SYS_ID_GOES_HERE'
Get a list of system tables [Read more...]
SELECT * FROM SystemTables
Get a list of user tables [Read more...]
Get columns of a specific table [Read more...]
SELECT * FROM get_table_columns
where name = 'TableNameGoesHere'
Get all columns of all tables (can be slow) [Read more...]
SELECT * FROM get_table_columns_admin --requires readonly permissions on sys_dictionary and sys_db_object tables
Use 'generic_request' endpoint to get Accounts [Read more...]
Use 'generic_request' endpoint to get Accounts (or access another ServiceNow API). Use ServiceNow API Reference and Query Builder to build custom requests.
SELECT *
FROM generic_request
WITH(
Url='/v2/table/incident?sysparm_limit=10000&sysparm_query=numberLIKE0001^ORDERBYsys_created_on',
Filter='$.result',
Meta='sys_id:string;number:string;business_stc:int', --//comment this to see all columns (SLOW)
PagingMode='ByResponseHeaderRfc5988',
--Method='GET',
--Body='{your POST body}'
)
-- Request URL should be partial like this one:
-- /v2/some_table?some-parameter=somevalue1&some-parameter=somevalue2