Edits history of script submission #22777 for ' Change Purchase Order Status (coupa)'

  • bunnative
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    
    /**
     * Change Purchase Order Status
     * Issue, close, cancel, or reopen a purchase order via its status transition endpoints.
     */
    export async function main(
      auth: RT.Coupa,
      purchase_order_id: number,
      action: "issue" | "close" | "cancel" | "reopen"
    ) {
      const base = auth.instance_url.replace(/\/+$/, "")
      const url = new URL(
        `${base}/api/purchase_orders/${purchase_order_id}/${action}`
      )
    
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          Authorization: `Bearer ${auth.token}`,
          Accept: "application/json",
        },
      })
    
      if (!response.ok) {
        throw new Error(`${response.status} ${await response.text()}`)
      }
    
      if (response.status === 204) return { success: true }
      return await response.json()
    }
    

    Submitted by hugo989 3 hours ago