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
| Value | Description |
|---|---|
| Table name | The record's table (for example, incident) |
| Record sys_id | The record to update |
| Fields | The 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
SetValueare serialized into the request — there's no read-modify-write cycle to race with other writers on untouched fields. Puton a wrongsys_idreturnscore.NotFoundError; match it witherrors.As(error handling).
Next steps
- Delete a record: Remove records you no longer need.
- Error handling: Handle not-found and validation failures cleanly.