0

Update Purchase Order

by
Published today

Update a purchase order by ID, e.g. flag it as exported. Field names use dashes, e.g. ship-to-attention.

Script coupa Verified

The script

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

3
/**
4
 * Update Purchase Order
5
 * Update a purchase order by ID, e.g. flag it as exported. Field names use dashes, e.g. ship-to-attention.
6
 */
7
export async function main(
8
  auth: RT.Coupa,
9
  purchase_order_id: number,
10
  body: { [key: string]: any }
11
) {
12
  const base = auth.instance_url.replace(/\/+$/, "")
13
  const url = new URL(`${base}/api/purchase_orders/${purchase_order_id}`)
14

15
  const response = await fetch(url, {
16
    method: "PUT",
17
    headers: {
18
      Authorization: `Bearer ${auth.token}`,
19
      "Content-Type": "application/json",
20
      Accept: "application/json",
21
    },
22
    body: JSON.stringify(body),
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