Edits history of script submission #22724 for ' Delete Attachment (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}`)}`
    }
    
    /**
     * Delete Attachment
     * Delete an attachment by its sys_id. ServiceNow returns 204 No Content on success, so this returns {success, sys_id}.
     */
    export async function main(auth: RT.Servicenow, attachment_sys_id: string) {
      const response = await fetch(
        `${auth.instance_url}/api/now/attachment/${attachment_sys_id}`,
        {
          method: "DELETE",
          headers: {
            Authorization: authHeader(auth),
            Accept: "application/json",
          },
        }
      )
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      return { success: true, sys_id: attachment_sys_id }
    }
    

    Submitted by hugo989 5 days ago