0

List Attachments

by
Published 4 days ago

List attachment metadata, optionally filtered with an encoded query (e.g. table_name=incident^table_sys_id=<id>).

Script servicenow Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 5 days ago
1
//native
2

3
function authHeader(auth: RT.Servicenow) {
4
  return auth.token
5
    ? `Bearer ${auth.token}`
6
    : `Basic ${btoa(`${auth.username}:${auth.password}`)}`
7
}
8

9
/**
10
 * List Attachments
11
 * List attachment metadata (sys_id, file_name, content_type, size, download_link). Filter with an encoded query, e.g. table_name=incident^table_sys_id=<record sys_id>.
12
 */
13
export async function main(
14
  auth: RT.Servicenow,
15
  query: string | undefined,
16
  limit: number | undefined
17
) {
18
  const url = new URL(`${auth.instance_url}/api/now/attachment`)
19
  if (query !== undefined && query !== "") {
20
    url.searchParams.append("sysparm_query", query)
21
  }
22
  url.searchParams.append("sysparm_limit", String(limit ?? 100))
23

24
  const response = await fetch(url, {
25
    method: "GET",
26
    headers: {
27
      Authorization: authHeader(auth),
28
      Accept: "application/json",
29
    },
30
  })
31

32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35

36
  return await response.json()
37
}
38