0

Update a purchase order using a custom field's unique value

by
Published Oct 17, 2025

A custom field will have unique values if it's configured to not accept duplicate values.

Script zoho Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Zoho = {
3
  token: string;
4
};
5
/**
6
 * Update a purchase order using a custom field's unique value
7
 * A custom field will have unique values if it's configured to not accept duplicate values.
8
 */
9
export async function main(
10
  auth: Zoho,
11
  organization_id: string | undefined,
12
  X_Unique_Identifier_Key: string,
13
  X_Unique_Identifier_Value: string,
14
  body: {
15
    vendor_id: string;
16
    currency_id?: string;
17
    contact_persons?: string[];
18
    purchaseorder_number?: string;
19
    gst_treatment?: string;
20
    tax_treatment?: {};
21
    gst_no?: string;
22
    source_of_supply?: string;
23
    destination_of_supply?: string;
24
    place_of_supply?: string;
25
    pricebook_id?: string;
26
    reference_number?: string;
27
    discount?: string;
28
    discount_account_id?: string;
29
    is_discount_before_tax?: false | true;
30
    billing_address_id?: number;
31
    crm_owner_id?: string;
32
    crm_custom_reference_id?: number;
33
    template_id?: string;
34
    date?: string;
35
    delivery_date?: string;
36
    due_date?: string;
37
    exchange_rate?: number;
38
    is_inclusive_tax?: false | true;
39
    notes?: string;
40
    notes_default?: string;
41
    terms?: string;
42
    terms_default?: string;
43
    ship_via?: string;
44
    delivery_org_address_id?: string;
45
    delivery_customer_id?: string;
46
    attention?: string;
47
    vat_treatment?: string;
48
    is_update_customer?: string;
49
    salesorder_id?: number;
50
    location_id?: string;
51
    line_items: {
52
      line_item_id?: string;
53
      item_id?: string;
54
      account_id?: string;
55
      name?: string;
56
      description?: string;
57
      unit?: string;
58
      location_id?: string;
59
      hsn_or_sac?: string;
60
      reverse_charge_tax_id?: string;
61
      rate?: number;
62
      quantity?: string;
63
      item_order?: number;
64
      tax_id?: string;
65
      tds_tax_id?: string;
66
      tax_treatment_code?: string;
67
      tax_exemption_code?: string;
68
      tax_exemption_id?: string;
69
      acquisition_vat_id?: string;
70
      reverse_charge_vat_id?: string;
71
      item_custom_fields?: { index?: number; value?: string }[];
72
      tags?: { tag_id?: number; tag_option_id?: number }[];
73
      project_id?: string;
74
    }[];
75
    custom_fields?: { customfield_id?: number; value?: string }[];
76
    documents?: { document_id?: number; file_name?: string }[];
77
  },
78
  X_Upsert?: string,
79
) {
80
  const url = new URL(`https://www.zohoapis.com/books/v3/purchaseorders`);
81
  for (const [k, v] of [["organization_id", organization_id]]) {
82
    if (v !== undefined && v !== "" && k !== undefined) {
83
      url.searchParams.append(k, v);
84
    }
85
  }
86
  const response = await fetch(url, {
87
    method: "PUT",
88
    headers: {
89
      "X-Unique-Identifier-Key": X_Unique_Identifier_Key,
90
      "X-Unique-Identifier-Value": X_Unique_Identifier_Value,
91
      ...(X_Upsert ? { "X-Upsert": X_Upsert } : {}),
92
      "Content-Type": "application/json",
93
      Authorization: "Zoho-oauthtoken " + auth.token,
94
    },
95
    body: JSON.stringify(body),
96
  });
97
  if (!response.ok) {
98
    const text = await response.text();
99
    throw new Error(`${response.status} ${text}`);
100
  }
101
  return await response.json();
102
}
103