0

Update an expense 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 expense 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
    account_id: string;
16
    date: string;
17
    amount: number;
18
    tax_id?: string;
19
    source_of_supply?: string;
20
    destination_of_supply?: string;
21
    place_of_supply?: string;
22
    hsn_or_sac?: string;
23
    gst_no?: string;
24
    reverse_charge_tax_id?: string;
25
    line_items?: {
26
      line_item_id?: string;
27
      account_id?: string;
28
      description?: string;
29
      amount?: number;
30
      tax_id?: string;
31
      item_order?: string;
32
      product_type?: string;
33
      acquisition_vat_id?: string;
34
      reverse_charge_vat_id?: string;
35
      reverse_charge_tax_id?: string;
36
      tax_exemption_code?: string;
37
      tax_exemption_id?: string;
38
      location_id?: string;
39
    }[];
40
    location_id?: string;
41
    taxes?: { tax_id?: string; tax_amount?: number }[];
42
    is_inclusive_tax?: false | true;
43
    is_billable?: false | true;
44
    reference_number?: string;
45
    description?: string;
46
    customer_id?: string;
47
    currency_id?: string;
48
    exchange_rate?: number;
49
    project_id?: string;
50
    mileage_type?: string;
51
    vat_treatment?: string;
52
    tax_treatment?: string;
53
    product_type?: string;
54
    acquisition_vat_id?: string;
55
    reverse_charge_vat_id?: string;
56
    start_reading?: number;
57
    end_reading?: number;
58
    distance?: string;
59
    mileage_unit?: string;
60
    mileage_rate?: number;
61
    employee_id?: string;
62
    vehicle_type?: string;
63
    can_reclaim_vat_on_mileage?: string;
64
    fuel_type?: string;
65
    engine_capacity_range?: string;
66
    paid_through_account_id: string;
67
    vendor_id?: string;
68
    custom_fields?: string[];
69
  },
70
  X_Upsert?: string,
71
) {
72
  const url = new URL(`https://www.zohoapis.com/books/v3/expenses`);
73
  for (const [k, v] of [["organization_id", organization_id]]) {
74
    if (v !== undefined && v !== "" && k !== undefined) {
75
      url.searchParams.append(k, v);
76
    }
77
  }
78
  const response = await fetch(url, {
79
    method: "PUT",
80
    headers: {
81
      "X-Unique-Identifier-Key": X_Unique_Identifier_Key,
82
      "X-Unique-Identifier-Value": X_Unique_Identifier_Value,
83
      ...(X_Upsert ? { "X-Upsert": X_Upsert } : {}),
84
      "Content-Type": "application/json",
85
      Authorization: "Zoho-oauthtoken " + auth.token,
86
    },
87
    body: JSON.stringify(body),
88
  });
89
  if (!response.ok) {
90
    const text = await response.text();
91
    throw new Error(`${response.status} ${text}`);
92
  }
93
  return await response.json();
94
}
95