0

Update an invoice 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 invoice 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
    customer_id: string;
16
    currency_id?: string;
17
    contact_persons?: string[];
18
    invoice_number?: string;
19
    place_of_supply?: string;
20
    vat_treatment?: string;
21
    tax_treatment?: string;
22
    is_reverse_charge_applied?: false | true;
23
    gst_treatment?: string;
24
    cfdi_usage?: string;
25
    cfdi_reference_type?: string;
26
    reference_invoice_id?: string;
27
    gst_no?: string;
28
    reference_number?: string;
29
    template_id?: string;
30
    date?: string;
31
    payment_terms?: number;
32
    payment_terms_label?: string;
33
    due_date?: string;
34
    discount?: number;
35
    is_discount_before_tax?: false | true;
36
    discount_type?: string;
37
    is_inclusive_tax?: false | true;
38
    exchange_rate?: number;
39
    recurring_invoice_id?: string;
40
    invoiced_estimate_id?: string;
41
    salesperson_name?: string;
42
    custom_fields?: { customfield_id?: number; value?: string }[];
43
    location_id?: string;
44
    line_items: {
45
      item_id: string;
46
      project_id?: string;
47
      time_entry_ids?: string[];
48
      product_type?: string;
49
      hsn_or_sac?: string;
50
      sat_item_key_code?: string;
51
      unitkey_code?: string;
52
      location_id?: string;
53
      expense_id?: string;
54
      expense_receipt_name?: string;
55
      name?: string;
56
      description?: string;
57
      item_order?: number;
58
      bcy_rate?: number;
59
      rate?: number;
60
      quantity?: number;
61
      unit?: string;
62
      discount_amount?: number;
63
      tags?: { tag_id?: string; tag_option_id?: string }[];
64
      discount?: number;
65
      tax_id?: string;
66
      tds_tax_id?: string;
67
      tax_name?: string;
68
      tax_type?: string;
69
      tax_percentage?: number;
70
      tax_treatment_code?: string;
71
      header_name?: string;
72
      header_id?: string;
73
    }[];
74
    payment_options?: {
75
      payment_gateways?: {
76
        configured?: false | true;
77
        additional_field1?: string;
78
        gateway_name?: string;
79
      }[];
80
    };
81
    allow_partial_payments?: false | true;
82
    custom_body?: string;
83
    custom_subject?: string;
84
    notes?: string;
85
    terms?: string;
86
    shipping_charge?: string;
87
    adjustment?: number;
88
    adjustment_description?: string;
89
    reason?: string;
90
    tax_authority_id?: string;
91
    tax_exemption_id?: string;
92
    avatax_use_code?: string;
93
    avatax_exempt_no?: string;
94
    tax_id?: string;
95
    expense_id?: string;
96
    salesorder_item_id?: string;
97
    avatax_tax_code?: string;
98
    line_item_id?: string;
99
  },
100
  X_Upsert?: string,
101
) {
102
  const url = new URL(`https://www.zohoapis.com/books/v3/invoices`);
103
  for (const [k, v] of [["organization_id", organization_id]]) {
104
    if (v !== undefined && v !== "" && k !== undefined) {
105
      url.searchParams.append(k, v);
106
    }
107
  }
108
  const response = await fetch(url, {
109
    method: "PUT",
110
    headers: {
111
      "X-Unique-Identifier-Key": X_Unique_Identifier_Key,
112
      "X-Unique-Identifier-Value": X_Unique_Identifier_Value,
113
      ...(X_Upsert ? { "X-Upsert": X_Upsert } : {}),
114
      "Content-Type": "application/json",
115
      Authorization: "Zoho-oauthtoken " + auth.token,
116
    },
117
    body: JSON.stringify(body),
118
  });
119
  if (!response.ok) {
120
    const text = await response.text();
121
    throw new Error(`${response.status} ${text}`);
122
  }
123
  return await response.json();
124
}
125