Querying¶
The SDK provides a powerful query builder to construct complex ServiceNow Encoded Queries in a type-safe and readable way. This helps filter table records precisely and minimize unnecessary data transfer.
Note: This is a preview feature currently under active development.
Understand encoded queries¶
ServiceNow uses a specific syntax for filtering records, known as Encoded Queries. These queries are strings consisting of field names, operators, and values.
Key concepts¶
- Operators: Used to compare field values, for example,
=,!=,LIKE,STARTSWITH. - Logical Operators: Used to combine multiple conditions (
^for and [conjunction],^ORfor or [disjunction]). - Encoded String: The final output of the query builder that's passed to the Table API.
Build a query¶
To build a query, use the query2 package. It provides methods for different
field types and logical operations.
q := query2.String("short_description").Contains("System").
And(query2.Number("priority").Is(1)).String()
fmt.Println(q) // Output: short_descriptionLIKESystem^priority=1
Use a query with the Table API¶
Once you've built a query string, you can use it in the request configuration for table operations.
// Build the query
q2 := query2.Boolean("active").Is(true).
And(query2.String("priority").
IsOneOf("1", "2")).String()
params := &tableapi.TableRequestBuilder2GetQueryParameters{
Query: q2,
}
config := &tableapi.TableRequestBuilder2GetRequestConfiguration{
QueryParameters: params,
}
response, err := client.Now2().TableV2("{TableName}").Get(ctx, config)
if err != nil {
log.Fatal(err)
}
Next steps¶
- Table Operations: Learn more about the operations where you'll use these queries.
- Pagination: Learn how to handle results when your query returns many records.