0

Insert Into Import Set

by
Published 4 days ago

Push a row into an import set staging table; the transform map runs synchronously. Inspect result[].status.

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
 * Insert Into Import Set
11
 * Push a row into an import set staging table; the associated transform map runs synchronously. Inspect result[].status (inserted/updated/ignored/error) — a transform error returns 200/201 with status "error".
12
 */
13
export async function main(
14
  auth: RT.Servicenow,
15
  staging_table: string,
16
  body: { [key: string]: any }
17
) {
18
  const url = new URL(`${auth.instance_url}/api/now/import/${staging_table}`)
19

20
  const response = await fetch(url, {
21
    method: "POST",
22
    headers: {
23
      Authorization: authHeader(auth),
24
      "Content-Type": "application/json",
25
      Accept: "application/json",
26
    },
27
    body: JSON.stringify(body),
28
  })
29

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

34
  return await response.json()
35
}
36