0

Change Purchase Order Status

by
Published today

Issue, close, cancel, or reopen a purchase order via its status transition endpoints.

Script coupa Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Change Purchase Order Status
5
 * Issue, close, cancel, or reopen a purchase order via its status transition endpoints.
6
 */
7
export async function main(
8
  auth: RT.Coupa,
9
  purchase_order_id: number,
10
  action: "issue" | "close" | "cancel" | "reopen"
11
) {
12
  const base = auth.instance_url.replace(/\/+$/, "")
13
  const url = new URL(
14
    `${base}/api/purchase_orders/${purchase_order_id}/${action}`
15
  )
16

17
  const response = await fetch(url, {
18
    method: "PUT",
19
    headers: {
20
      Authorization: `Bearer ${auth.token}`,
21
      Accept: "application/json",
22
    },
23
  })
24

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

29
  if (response.status === 204) return { success: true }
30
  return await response.json()
31
}
32