Skip to main content
Version: v2.0

Update a record

Change specific fields on an existing record, leaving everything else untouched.

When to use this pattern

  • You're progressing a record through a workflow (state changes, assignments, work notes)
  • You're writing back results from an external process to the record that requested it
  • You have the record's sys_id — if you don't, find it first

Required values

ValueDescription
Table nameThe record's table (for example, incident)
Record sys_idThe record to update
FieldsThe field values to change

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: Build the update — set only the fields to change;
// everything else on the record is left untouched
update := tableapi.NewTableRecord()
if err := update.SetValue("short_description", "Updated description"); err != nil {
log.Fatal(err)
}

// Step 3: Apply it to the record by sys_id
sysID := "{SysID}"
response, err := client.Now().Table("{TableName}").ByID(sysID).Put(context.Background(), update, nil)
if err != nil {
log.Fatalf("unable to update record: %v", err)
}

if _, err := response.GetResult(); err != nil {
log.Fatalf("unable to read result: %v", err)
}

fmt.Printf("Updated record %s\n", sysID)
}

Tips

  • Only the fields you SetValue are serialized into the request — there's no read-modify-write cycle to race with other writers on untouched fields.
  • Put on a wrong sys_id returns core.NotFoundError; match it with errors.As (error handling).

Next steps

Was this page helpful?