List and query records
Fetch the records matching a filter and read fields from each result.
When to use this pattern
- You're pulling a working set of records into another system (open incidents, pending approvals, recent changes)
- You need server-side filtering so you only transfer the records you care about
- You're reading field values out of the results, not just counting them (for counts and aggregates, use the Aggregation API)
Required values
| Value | Description |
|---|---|
| Table name | The table to read (for example, incident) |
| Encoded query | The server-side filter (for example, active=true^priority=1) |
Example
package main
import (
"context"
"fmt"
"log"
servicenow "github.com/michaeldcanady/servicenow-sdk-go/v2"
"github.com/michaeldcanady/servicenow-sdk-go/v2/credentials"
tableapi "github.com/michaeldcanady/servicenow-sdk-go/v2/tableapi"
)
func main() {
// Step 1: Authenticate and initialize the client
cred := credentials.NewBasicProvider("{username}", "{password}")
client, err := servicenow.NewServiceNowServiceClient(
servicenow.WithAuthenticationProvider(cred),
servicenow.WithInstance("{instance}"),
)
if err != nil {
log.Fatalf("failed to initialize client: %v", err)
}
// Step 2: Configure the query — an encoded query filters server-side
tableQuery := "active=true^priority=1"
limit := int32(10)
config := &tableapi.TableRequestBuilderGetRequestConfiguration{
QueryParameters: &tableapi.TableRequestBuilderGetQueryParameters{
Query: &tableQuery,
Limit: &limit,
},
}
// Step 3: Fetch the matching records
response, err := client.Now().Table("{TableName}").Get(context.Background(), config)
if err != nil {
log.Fatalf("unable to list records: %v", err)
}
records, err := response.GetResult()
if err != nil {
log.Fatalf("unable to read results: %v", err)
}
// Step 4: Read a field from each record
for _, record := range records {
element, err := record.Get("number")
if err != nil {
log.Fatal(err)
}
value, err := element.GetValue()
if err != nil {
log.Fatal(err)
}
number, err := value.GetStringValue()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Record: %s\n", *number)
}
}
Tips
- Compose non-trivial filters with the query builder instead of concatenating encoded-query strings.
- Reading a field is a three-step unwrap (
Get→GetValue→GetStringValue) — Core Concepts explains why. Limitcaps one page, not the result set — iterate with pagination when the match count can exceed it.
Next steps
- Pagination: Walk every page of a large result set.
- Update a record: Act on the records you found.