0

Delete Attachment

by
Published 4 days ago

Delete an attachment by sys_id. Returns {success, sys_id} (ServiceNow replies 204 No Content).

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
 * Delete Attachment
11
 * Delete an attachment by its sys_id. ServiceNow returns 204 No Content on success, so this returns {success, sys_id}.
12
 */
13
export async function main(auth: RT.Servicenow, attachment_sys_id: string) {
14
  const response = await fetch(
15
    `${auth.instance_url}/api/now/attachment/${attachment_sys_id}`,
16
    {
17
      method: "DELETE",
18
      headers: {
19
        Authorization: authHeader(auth),
20
        Accept: "application/json",
21
      },
22
    }
23
  )
24

25
  if (!response.ok) {
26
    throw new Error(`${response.status} ${await response.text()}`)
27
  }
28

29
  return { success: true, sys_id: attachment_sys_id }
30
}
31