Skip to content

Create attachment file

Overview

Upload file of any supported content type. Requires you to provide the table sys id, table name, and file name via the request query parameters.

Path parameters

Name Description
baseurl The absolute base URI for the request (this is the same for all requests) including: the schema, the domain, and a path fragment.

Optional query parameters

Name Type Possible values Description
EncryptionContext *string N/A sys_id of an encryption context record.

Required query parameters

Name Type Possible values Description
FileName *string N/A Name to provided file.
TableName *string N/A Name of the designated table which contains the record to attach the file to.
TableSysID *string N/A Specifies the sys_id of the record in the designated table to attach the file to.

Examples

package main


import (
    "context"
    "fmt"
    "log"
    "os"

    servicenowsdkgo "github.com/michaeldcanady/servicenow-sdk-go"
    attachmentapi "github.com/michaeldcanady/servicenow-sdk-go/attachment-api"
    "github.com/michaeldcanady/servicenow-sdk-go/credentials"
    abstractions "github.com/microsoft/kiota-abstractions-go"
)

func main() {
    // Step 1: Create credentials

    cred := credentials.NewBasicProvider("{username}", "{password}")


    // Step 2: Initialize client

    client, err := servicenowsdkgo.NewServiceNowServiceClient(
        servicenowsdkgo.WithAuthenticationProvider(cred),
        servicenowsdkgo.WithURL("https://{instance}.service-now.com"),
    )
    if err != nil {
        log.Fatal(err)
    }



    // Build media type
    dataContentType := "text/plain"
    data := []byte("this is example data")
    media := attachmentapi.NewMedia(dataContentType, data)

    create_file_table_sys_id := "{SysID}"
    create_file_table_name := "{TableName}"
    create_file_file_name := "example.txt"

    create_file_config := &attachmentapi.AttachmentFileRequestBuilderPostRequestConfiguration{
        QueryParameters: &attachmentapi.AttachmentFileRequestBuilderPostQueryParameters{
            TableSysID: &create_file_table_sys_id, // required
            TableName:  &create_file_table_name,   // required
            FileName:   &create_file_file_name,    // required
        },
        // Optional configurations
    }

    create_file_response, err := client.Now2().Attachment2().File().Post(context.Background(), media, create_file_config)
    if err != nil {
        log.Fatal(err)
    }

}
package main


import (
    "context"
    "fmt"
    "log"
    "os"

    servicenowsdkgo "github.com/michaeldcanady/servicenow-sdk-go"
    attachmentapi "github.com/michaeldcanady/servicenow-sdk-go/attachment-api"
    "github.com/michaeldcanady/servicenow-sdk-go/credentials"
    abstractions "github.com/microsoft/kiota-abstractions-go"
)

func main() {
    // Step 1: Create credentials

    cred := credentials.NewBasicProvider("{username}", "{password}")


    // Step 2: Initialize client

    client, err := servicenowsdkgo.NewServiceNowServiceClient(
        servicenowsdkgo.WithAuthenticationProvider(cred),
        servicenowsdkgo.WithURL("https://{instance}.service-now.com"),
    )
    if err != nil {
        log.Fatal(err)
    }



    // Step 3: Define raw URL
    file_create_rawURL := "https://https://{instance}.service-now.com/api/now/attachment/file"

    // Step 4: Build media type
    file_create_dataContentType := "text/plain"
    file_create_data := []byte("this is example data")
    file_create_media := attachmentapi.NewMedia(file_create_dataContentType, file_create_data)

    file_create_table_sys_id := "{SysID}"
    file_create_table_name := "{TableName}"
    file_create_file_name := "example.txt"

    // Step 5: Configure request
    file_create_std_config := &attachmentapi.AttachmentFileRequestBuilderPostRequestConfiguration{
        QueryParameters: &attachmentapi.AttachmentFileRequestBuilderPostQueryParameters{
            TableSysID: &file_create_table_sys_id, // required
            TableName:  &file_create_table_name,   // required
            FileName:   &file_create_file_name,    // required
        },
        // Optional configurations
    }

    // Step 6: Build request
    file_create_builder := attachmentapi.NewAttachmentFileRequestBuilder(file_create_rawURL, client.RequestAdapter)

    file_create_std_response, err := file_create_builder.Post(context.Background(), file_create_media, file_create_std_config)
    if err != nil {
        log.Fatal(err)
    }

}