0

Update an item 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 an item 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
  X_Upsert?: string,
15
  body: {
16
    name: string;
17
    rate: number;
18
    description?: string;
19
    tax_id?: string;
20
    purchase_tax_rule_id?: string;
21
    sales_tax_rule_id?: string;
22
    tax_percentage?: string;
23
    hsn_or_sac?: string;
24
    sat_item_key_code?: string;
25
    unitkey_code?: string;
26
    sku?: string;
27
    product_type?: string;
28
    is_taxable?: false | true;
29
    tax_exemption_id?: string;
30
    purchase_tax_exemption_id?: string;
31
    account_id?: string;
32
    avatax_tax_code?: string;
33
    avatax_use_code?: string;
34
    item_type?: string;
35
    purchase_description?: string;
36
    purchase_rate?: string;
37
    purchase_account_id?: string;
38
    inventory_account_id?: string;
39
    vendor_id?: string;
40
    reorder_level?: string;
41
    locations?: {
42
      location_id?: string;
43
      initial_stock?: string;
44
      initial_stock_rate?: string;
45
    }[];
46
    item_tax_preferences?: { tax_id?: string; tax_specification?: string }[];
47
    custom_fields?: { customfield_id?: number; value?: string }[];
48
  },
49
) {
50
  const url = new URL(`https://www.zohoapis.com/books/v3/items`);
51
  for (const [k, v] of [["organization_id", organization_id]]) {
52
    if (v !== undefined && v !== "") {
53
      url.searchParams.append(k, v);
54
    }
55
  }
56
  const response = await fetch(url, {
57
    method: "PUT",
58
    headers: {
59
      "X-Unique-Identifier-Key": X_Unique_Identifier_Key,
60
      "X-Unique-Identifier-Value": X_Unique_Identifier_Value,
61
      ...(X_Upsert ? { "X-Upsert": X_Upsert } : {}),
62
      "Content-Type": "application/json",
63
      Authorization: "Zoho-oauthtoken " + auth.token,
64
    },
65
    body: JSON.stringify(body),
66
  });
67
  if (!response.ok) {
68
    const text = await response.text();
69
    throw new Error(`${response.status} ${text}`);
70
  }
71
  return await response.json();
72
}
73