Edits history of script submission #22730 for ' Insert Into Import Set (servicenow)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    function authHeader(auth: RT.Servicenow) {
      return auth.token
        ? `Bearer ${auth.token}`
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`
    }
    
    /**
     * Insert Into Import Set
     * 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".
     */
    export async function main(
      auth: RT.Servicenow,
      staging_table: string,
      body: { [key: string]: any }
    ) {
      const url = new URL(`${auth.instance_url}/api/now/import/${staging_table}`)
    
      const response = await fetch(url, {
        method: "POST",
        headers: {
          Authorization: authHeader(auth),
          "Content-Type": "application/json",
          Accept: "application/json",
        },
        body: JSON.stringify(body),
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return await response.json()
    }
    

    Submitted by hugo989 5 days ago