- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2022 10:00 PM
Hi,
I am a newbie to REST API.
I need to use ServiceNow's Attachment API and PowerShell to upload a file to a field of type "File Attachment" in a certain table.
How do I go about writing the code, etc.?
Table name:x_540069_sn21_systemid
"File Attachment" type fields name:u_nw_kouseizu
target record:9fb8d7badb917010c42a8e4748961977
File name to upload:File Attachment test.xlsx
Can someone please tell me how to do this?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
-
Team Development

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 03:26 AM
Fixed the script and was able to execute it from my Windows PC to successfully attach a file to a form in my ServiceNow PDI.
# Eg. User name="admin", Password="admin" for this code sample.
$user = "<username>"
$pass = "<password>"
# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$filePath = "<path to file>/test1.xlsx"
# Specify endpoint uri
$uri = "https://<instance name>.service-now.com/api/now/attachment/file?table_name=<table name>&table_sys_id=<sys_id of record to attach file>&file_name=test1.xlsx"
# Specify HTTP method
$method = "post"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -InFile $filePath -Uri $uri
# Print response
$response.RawContent

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2022 03:26 AM
Fixed the script and was able to execute it from my Windows PC to successfully attach a file to a form in my ServiceNow PDI.
# Eg. User name="admin", Password="admin" for this code sample.
$user = "<username>"
$pass = "<password>"
# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$filePath = "<path to file>/test1.xlsx"
# Specify endpoint uri
$uri = "https://<instance name>.service-now.com/api/now/attachment/file?table_name=<table name>&table_sys_id=<sys_id of record to attach file>&file_name=test1.xlsx"
# Specify HTTP method
$method = "post"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -InFile $filePath -Uri $uri
# Print response
$response.RawContent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2022 06:57 PM
Thank you very much.
For now, I was able to confirm that I can upload files to the form.
>'query to find the record in sys_attachment table and rename the table name by prefixing it with "ZZ_YY"'
Does this mean that I should use 'Glide Record' in the 'sys_attachment' table?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2022 07:27 PM
No. To show the attachment in a field, it's necessary to write a script to update reference field "u_nw_kouseizu" in table "x_540069_sn21_systemid".
There are 2 options:
1. call the above api to upload the file to sys_attachment table and then call scripted api to updated the x_540069_sn21_systemid table record. Scripted api needs to be developed.
2. Create a scripted api to accept the file and to update the x_540069_sn21_systemid in one api call. In this case, use RESTMessageV2 class's .saveResponseBodyAsAttachment method in Scripted REST API to receive the file and add it to sys_attachment table.
To get further information on how to do this, it'll be better to create a new question so others will also be able to answer. Other people probably won't answer on this thread because I've already supplied a PowerShell script to upload an excel file as the original question asked. I'm going into a meeting so I won't be able to answer for few hours.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2022 08:32 PM
I got it.
I've never heard of "scripted api", so I'll start by looking into it.
Thank you for taking the time to help me.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-27-2022 09:12 PM
Helpful content.