Create a record
Insert a new record into a table and read back the sys_id ServiceNow
assigned to it.
When to use this pattern
- You're opening incidents, requests, or cases from an external system
- You're importing or synchronizing data into a ServiceNow table
- You need the new record's
sys_idto link follow-up work (attachments, updates, references)
Required values
| Value | Description |
|---|---|
| Table name | The table to insert into (for example, incident) |
| Fields | The field values for the new record |
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 record — only fields you set are sent
newRecord := tableapi.NewTableRecord()
if err := newRecord.SetValue("short_description", "System is down"); err != nil {
log.Fatal(err)
}
if err := newRecord.SetValue("priority", "1"); err != nil {
log.Fatal(err)
}
// Step 3: Create it
response, err := client.Now().Table("{TableName}").Post(context.Background(), newRecord, nil)
if err != nil {
log.Fatalf("unable to create record: %v", err)
}
// Step 4: Read the new record's sys_id from the response
created, err := response.GetResult()
if err != nil {
log.Fatalf("unable to read result: %v", err)
}
sysID, err := created.GetSysID()
if err != nil {
log.Fatalf("unable to read sys_id: %v", err)
}
fmt.Printf("Created record with sys_id: %s\n", *sysID)
}
Tips
- Only fields you
SetValueare sent — ServiceNow fills the rest with the table's defaults (why). - Field values are strings as ServiceNow's forms would accept them
(
"1"for a priority, not1). - Keep the returned
sys_id; every follow-up operation on the record needs it.
Next steps
- Update a record: Change the record you just created.
- Upload an attachment: Attach a file to it.