//native
function authHeader(auth: RT.Servicenow) {
return auth.token
? `Bearer ${auth.token}`
: `Basic ${btoa(`${auth.username}:${auth.password}`)}`
}
/**
* Get Change Next States
* List the states a change request can move to next, including its current state, through the Change Management API (GET /api/sn_chg_rest/change/{sys_id}/nextstates, documented in the Yokohama and Brazil API references). For changes driven by a change model, each transition also says whether it is available and which conditions passed or failed; type-driven (legacy) changes only return the state list.
*/
export async function main(auth: RT.Servicenow, sys_id: string) {
const url = new URL(
`${auth.instance_url}/api/sn_chg_rest/change/${encodeURIComponent(sys_id)}/nextstates`
)
const response = await fetch(url, {
headers: {
Authorization: authHeader(auth),
Accept: "application/json",
},
})
if (!response.ok) {
throw new Error(`${response.status} ${await response.text()}`)
}
const { result } = (await response.json()) as {
result: {
available_states?: string[]
state_label?: { [state: string]: string }
state_transitions?: any[]
}
}
// The schema documents state_transitions as an array of transitions, but the
// documented example nests one array per target state: accept both.
const transitions = (result.state_transitions ?? []).flat().map((t: any) => ({
from_state: t.from_state,
to_state: t.to_state,
to_state_label: result.state_label?.[t.to_state],
display_value: t.display_value,
transition_available: t.transition_available,
automatic_transition: t.automatic_transition,
conditions: (t.conditions ?? []).map((c: any) => ({
name: c.condition?.name,
description: c.condition?.description,
passed: c.passed,
})),
}))
return {
available_states: (result.available_states ?? []).map((state) => ({
state,
label: result.state_label?.[state],
})),
transitions,
result,
}
}
Submitted by hugo989 7 hours ago