Skip to content

Table operations

The Table API lets you perform Create, Read, Update, and Delete (CRUD) operations on ServiceNow table records.

Understand the modalities

The SDK offers two distinct ways to build and execute requests.

  • Fluent: A method-chaining approach that's expressive and easy to read. That's preferred for most common tasks.
  • Standard: A more explicit approach that requires you to manage request builders and raw URLs. This is useful when you need to handle dynamic URLs or specific configurations not expressed fluently.

Get a single record

To retrieve a specific record, use the ById method in the fluent interface.

// Step 3: Configure request
getConfig := &tableapi.TableItemRequestBuilder2GetRequestConfiguration{
    QueryParameters: &tableapi.TableItemRequestBuilder2GetQueryParameters{
        // Optional configurations
    },
}

// Step 4: Execute request
getResponse, err := client.Now2().TableV2("{TableName}").ById("{SysID}").Get(context.Background(), getConfig)
if err != nil {
    log.Fatal(err)
}

List records

To list records from a table, call Get on the table resource.

// Get records from the 'incident' table
listGuideResponse, err := client.Now2().TableV2("{TableName}").Get(ctx, nil)
if err != nil {
    log.Fatalf("Error: %v", err)
}

results, err := listGuideResponse.GetResult()
if err != nil {
    log.Fatal(err)
}

for _, record := range results {
    num, err := record.Get("number")
    if err != nil {
        log.Fatal(err)
    }

    val, err := num.GetValue()
    if err != nil {
        log.Fatal(err)
    }
    strVal, err := val.GetStringValue()
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("Incident: %s\n", *strVal)
}

Create records

To create a new record, use the Post method and provide a TableRecord containing the field values.

newIncident := tableapi.NewTableRecord()
if err := newIncident.SetValue("short_description", "System is down"); err != nil {
    log.Fatal(err)
}
if err := newIncident.SetValue("priority", "1"); err != nil {
    log.Fatal(err)
}

create_guide_response, err := client.Now2().TableV2("{TableName}").Post(ctx, newIncident, nil)
if err != nil {
    log.Fatalf("Error: %v", err)
}

result, _ := create_guide_response.GetResult()

sysId, _ := result.GetSysID()
fmt.Printf("Created incident with sys_id: %s\n", *sysId)

Update records

To update an existing record, use the Put method with the record's sys_id and a TableRecord containing the updated fields.

sysIdStr := "{SysID}" // your record sys_id

updateGuideData := tableapi.NewTableRecord()
if err := updateGuideData.SetValue("short_description", "Updated description"); err != nil {
    log.Fatal(err)
}

update_guide_response, err := client.Now2().TableV2("{TableName}").ById(sysIdStr).Put(ctx, updateGuideData, nil)
if err != nil {
    log.Fatal(err)
}

Delete records

To delete a record, use the Delete method with the record's sys_id.

sysIdToDelete := "{SysID}"
err = client.Now2().TableV2("{TableName}").ById(sysIdToDelete).Delete(ctx, nil)
if err != nil {
    log.Fatal(err)
}

Query records

You can filter results by providing a query string in the request configuration. ServiceNow uses a specific query syntax (Encoded Queries).

params := &tableapi.TableRequestBuilder2GetQueryParameters{
    Query: "priority=1^active=true",
    Limit: 10,
}

config := &tableapi.TableRequestBuilder2GetRequestConfiguration{
    QueryParameters: params,
}

query_guide_response, err := client.Now2().TableV2("{TableName}").Get(ctx, config)
if err != nil {
    log.Fatal(err)
}

Next steps