Skip to main content
Version: v2.0

Attachments

The Attachment API lets you manage files associated with records in ServiceNow. You can list, retrieve, upload, and delete attachments using the SDK.

The pages in this section walk through complete, real-world tasks:

The sections below cover each basic operation.

List attachments

You can retrieve a list of all attachments or filter them based on criteria such as the table name or table sys_id.

// List all attachments
listGuideResponse, err := client.Now().Attachment().Get(ctx, nil)
if err != nil {
log.Fatal(err)
}

listGuideResults, _ := listGuideResponse.GetResult()
for _, attachment := range listGuideResults {
name, err := attachment.GetFileName()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Attachment: %s\n", *name)
}

Upload attachments

To upload a file and associate it with a specific record, use the File resource's Post method. You must provide the table name, the record's sys_id, and the file name in the query parameters.

file, err := os.Open("path/to/file.txt")
if err != nil {
log.Fatal(err)
}
defer func() { _ = file.Close() }()

// Upload attachment for an incident
guideTableName := "{TableName}"
guideTableSysId := "{SysID}"
guideFileName := "file.txt"

params := &attachmentapi.AttachmentFileRequestBuilderPostQueryParameters{
TableName: &guideTableName,
TableSysID: &guideTableSysId,
FileName: &guideFileName,
}

config := &attachmentapi.AttachmentFileRequestBuilderPostRequestConfiguration{
QueryParameters: params,
}

// Assuming 'file' can be used as media content
createGuideResponse, err := client.Now().Attachment().File().Post(ctx, nil, config) // Placeholder
if err != nil {
log.Fatal(err)
}

result, err := createGuideResponse.GetResult()
if err != nil {
log.Fatal(err)
}

id, err := result.GetSysID()
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created attachment with sys_id: %s\n", *id)

Download attachments

To download the content of an attachment, use the ByID and File resources to call the Get method.

sysIdToDownload := "{SysID}"

config2 := &attachmentapi.AttachmentItemFileRequestBuilderGetRequestConfiguration{}

downloadFile, err := client.Now().Attachment().ByID(sysIdToDownload).File().Get(ctx, config2)
if err != nil {
log.Fatal(err)
}

content, _ := downloadFile.GetContent()
// content is a []byte containing the file data

Delete attachments

To delete an attachment, use the ByID resource and call the Delete method.

// Step 3: Configure request
deleteConfig := &attachmentapi.AttachmentItemRequestBuilderDeleteRequestConfiguration{
// Optional configurations
}

err = client.Now().Attachment().ByID("{SysID}").Delete(context.Background(), deleteConfig)
if err != nil {
log.Fatal(err)
}

Next steps

  • Batch API: Learn how to perform multiple operations, including attachment management, in a single request.
  • Table Operations: Learn more about managing the records these attachments belong to.
Was this page helpful?