0

Update a bill 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 bill 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
    vat_treatment?: string;
18
    is_update_customer?: false | true;
19
    purchaseorder_ids?: string[];
20
    bill_number: string;
21
    documents?: { document_id?: number; file_name?: string }[];
22
    source_of_supply?: string;
23
    destination_of_supply?: string;
24
    place_of_supply?: string;
25
    permit_number?: string;
26
    gst_treatment?: string;
27
    tax_treatment?: string;
28
    gst_no?: string;
29
    pricebook_id?: string;
30
    reference_number?: string;
31
    date?: string;
32
    due_date?: string;
33
    payment_terms?: number;
34
    payment_terms_label?: string;
35
    recurring_bill_id?: string;
36
    exchange_rate?: number;
37
    is_item_level_tax_calc?: false | true;
38
    is_inclusive_tax?: false | true;
39
    adjustment?: number;
40
    adjustment_description?: string;
41
    custom_fields?: { index?: number; value?: string }[];
42
    line_items?: {
43
      purchaseorder_item_id?: string;
44
      line_item_id?: string;
45
      item_id?: string;
46
      name?: string;
47
      account_id?: string;
48
      description?: string;
49
      rate?: number;
50
      hsn_or_sac?: string;
51
      reverse_charge_tax_id?: string;
52
      location_id?: string;
53
      quantity?: number;
54
      tax_id?: string;
55
      tds_tax_id?: string;
56
      tax_treatment_code?: string;
57
      tax_exemption_id?: string;
58
      tax_exemption_code?: string;
59
      item_order?: number;
60
      product_type?: string;
61
      acquisition_vat_id?: string;
62
      reverse_charge_vat_id?: string;
63
      unit?: string;
64
      tags?: { tag_id?: number; tag_option_id?: number }[];
65
      is_billable?: false | true;
66
      project_id?: string;
67
      customer_id?: string;
68
      item_custom_fields?: {
69
        custom_field_id?: number;
70
        index?: number;
71
        value?: string;
72
        label?: string;
73
      }[];
74
      serial_numbers?: string[];
75
    }[];
76
    location_id?: string;
77
    taxes?: { tax_id?: string; tax_name?: string; tax_amount?: string }[];
78
    notes?: string;
79
    terms?: string;
80
    approvers?: { approver_id?: number; order?: number }[];
81
  },
82
  X_Upsert?: string,
83
) {
84
  const url = new URL(`https://www.zohoapis.com/books/v3/bills`);
85
  for (const [k, v] of [["organization_id", organization_id]]) {
86
    if (v !== undefined && v !== "" && k !== undefined) {
87
      url.searchParams.append(k, v);
88
    }
89
  }
90
  const response = await fetch(url, {
91
    method: "PUT",
92
    headers: {
93
      "X-Unique-Identifier-Key": X_Unique_Identifier_Key,
94
      "X-Unique-Identifier-Value": X_Unique_Identifier_Value,
95
      ...(X_Upsert ? { "X-Upsert": X_Upsert } : {}),
96
      "Content-Type": "application/json",
97
      Authorization: "Zoho-oauthtoken " + auth.token,
98
    },
99
    body: JSON.stringify(body),
100
  });
101
  if (!response.ok) {
102
    const text = await response.text();
103
    throw new Error(`${response.status} ${text}`);
104
  }
105
  return await response.json();
106
}
107