Querying tables in script
-
- UpdatedAug 1, 2024
- 7 minutes to read
- Xanadu
- Building applications
Using methods in the GlideRecord API, you can return all records from a table, return records from a table that satisfy specific conditions, or return records that include a string from a single table or from multiple tables in a text index group.
Query tables using the GlideRecord API. For API reference, see GlideRecord - Scoped.
Return all records from a table
Creating a GlideRecord creates a target
variable which is a GlideRecord
object for the incident table.
This script issues the query() to the database. Each call to next() would load the next record for processing.
Return records from a table that satisfy query conditions
target.addQuery('priority', 1);
.
This line indicates that you only want the records where the priority
field is equal to 1. In general, most queries that you want to perform are equality queries;
queries where you want to find records with a field equal to a value. For this reason, you
do not need to provide an equality operator. However, lets say you wanted to find all
incidents where the priority field is greater than 1. In this case, you
would provide the operator that you want to apply to the
query.Return records from a table that include a string
Use the '123TEXTQUERY321'
reserved name to search for string matches in all fields on a table. For example, this script returns records from the Incident table with field values that include the 'email'
string.
'123TEXTQUERY321'
is a reserved option for the name parameter in the addQuery()
method. You can use this option in an encoded query string. For example, instead of
gr.addQuery('123TEXTQUERY321', 'email');
, you can use gr.addEncodedQuery('123TEXTQUERY321=email')
.
String search is case-insensitive. The system returns the same results whether you search for email, Email, or EMAIL.
Return records from multiple tables in a text index group that include a string
Use the '123TEXTINDEXGROUP321'
reserved name to search for a string in a table from a text index group. This option returns results with relevancy scores computed using the text index group's settings.
You can create a similar query for each additional table in the 'portal' index group that you want to search, and then merge the individual queries' results, displaying the results with the highest relevancy scores first.
Because all of these search queries use the same text index group search settings, the relevancy scores for their results are all normalized consistently. If you searched the same set of tables without using the
gr.addQuery('123TEXTINDEXGROUP321', 'portal')
method, the individual queries' relevancy scores would be normalized differently and would not be a useful basis for sorting the merged result set.
'123TEXTINDEXGROUP321'
is a reserved option for the name parameter in the addQuery()
method. You can use this option in an encoded query string. For example, instead of
gr.addQuery('123TEXTINDEXGROUP321', 'portal');
, you can use gr.addEncodedQuery('123TEXTINDEXGROUP321=portal')
.
Multi-table string search is case-insensitive. The system returns the same results whether you search for email, Email, or EMAIL.
Available JavaScript operators
Describes the operators that can be used within an addQuery() request.
For additional information on the operators that are available for filters and queries, see Operators available for filters and queries.
For more information on the GlideRecord API and its available methods, see GlideRecord.
GlideRecord query examples
These examples demonstrate how to perform various GlideRecord queries.
query
update
insert
delete
Querying Service Catalog Tables
For additional information see GlideRecord.