Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

About Attachment API and "File Attachment" type fields

Koki
Tera Contributor

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?

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

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

View solution in original post

14 REPLIES 14

Sumit Maniktal1
Tera Expert

Check below topic for help on this

https://developer.servicenow.com/print_page.do?release=paris&category=null&identifier=c_AttachmentAPI&module=api

Already checked, can't see how to specify the field.

Hitoshi Ozawa
Giga Sage
Giga Sage

Haven't tested but something like below. Replace <username>,<password>, and <instance name>.

# 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('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')


# Specify endpoint uri
$uri = "https://<instance_name>.service-now.com/api/now/attachment/file?table_name=x_540069_sn21_systemid&table_sys_id=9fb8d7badb917010c42a8e4748961977&file_name=test.xlsx"

# Specify HTTP method
$method = "post"




# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri 

# Print response
$response.RawContent

It had been confirmed that such code could be created using the REST API Explorer.
However, I am unable to specify a field of type "File Attachment".

Also, when I run this code, I get an error and I have no idea why.