0

Update a contact 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 contact 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
    contact_name: string;
16
    company_name?: string;
17
    payment_terms?: number;
18
    payment_terms_label?: string;
19
    contact_type?: string;
20
    customer_sub_type?: string;
21
    currency_id?: string;
22
    opening_balances?: {
23
      location_id?: string;
24
      exchange_rate?: number;
25
      opening_balance_amount?: number;
26
    }[];
27
    credit_limit?: number;
28
    tags?: { tag_id?: string; tag_option_id?: string }[];
29
    website?: string;
30
    owner_id?: string;
31
    custom_fields?: { index?: number; value?: string; label?: string }[];
32
    billing_address?: {
33
      attention?: string;
34
      address?: string;
35
      street2?: string;
36
      state_code?: string;
37
      city?: string;
38
      state?: string;
39
      zip?: string;
40
      country?: string;
41
      fax?: string;
42
      phone?: string;
43
    };
44
    shipping_address?: {
45
      attention?: string;
46
      address?: string;
47
      street2?: string;
48
      state_code?: string;
49
      city?: string;
50
      state?: string;
51
      zip?: string;
52
      country?: string;
53
      fax?: string;
54
      phone?: string;
55
    };
56
    contact_persons?: {
57
      contact_person_id?: string;
58
      salutation?: string;
59
      first_name?: string;
60
      last_name?: string;
61
      email?: string;
62
      phone?: string;
63
      mobile?: string;
64
      designation?: string;
65
      department?: string;
66
      skype?: string;
67
      is_primary_contact?: false | true;
68
      enable_portal?: false | true;
69
    }[];
70
    default_templates?: {
71
      invoice_template_id?: string;
72
      estimate_template_id?: string;
73
      creditnote_template_id?: string;
74
      purchaseorder_template_id?: string;
75
      salesorder_template_id?: string;
76
      retainerinvoice_template_id?: string;
77
      paymentthankyou_template_id?: string;
78
      retainerinvoice_paymentthankyou_template_id?: string;
79
      invoice_email_template_id?: string;
80
      estimate_email_template_id?: string;
81
      creditnote_email_template_id?: string;
82
      purchaseorder_email_template_id?: string;
83
      salesorder_email_template_id?: string;
84
      retainerinvoice_email_template_id?: string;
85
      paymentthankyou_email_template_id?: string;
86
      retainerinvoice_paymentthankyou_email_template_id?: string;
87
    };
88
    notes?: string;
89
    vat_reg_no?: string;
90
    tax_reg_no?: string;
91
    country_code?: string;
92
    tax_treatment?: string;
93
    tax_exemption_certificate_number?: string;
94
    tax_regime?: string;
95
    legal_name?: string;
96
    is_tds_registered?: false | true;
97
    vat_treatment?: string;
98
    place_of_contact?: string;
99
    gst_no?: string;
100
    gst_treatment?: string;
101
    tax_authority_name?: string;
102
    avatax_exempt_no?: string;
103
    avatax_use_code?: string;
104
    tax_exemption_id?: string;
105
    tax_exemption_code?: string;
106
    tax_authority_id?: string;
107
    tax_id?: string;
108
    is_taxable?: false | true;
109
    facebook?: string;
110
    twitter?: string;
111
    track_1099?: false | true;
112
    tax_id_type?: string;
113
    tax_id_value?: string;
114
  },
115
  X_Upsert?: string,
116
) {
117
  const url = new URL(`https://www.zohoapis.com/books/v3/contacts`);
118
  for (const [k, v] of [["organization_id", organization_id]]) {
119
    if (v !== undefined && v !== "" && k !== undefined) {
120
      url.searchParams.append(k, v);
121
    }
122
  }
123
  const response = await fetch(url, {
124
    method: "PUT",
125
    headers: {
126
      "X-Unique-Identifier-Key": X_Unique_Identifier_Key,
127
      "X-Unique-Identifier-Value": X_Unique_Identifier_Value,
128
      ...(X_Upsert ? { "X-Upsert": X_Upsert } : {}),
129
      "Content-Type": "application/json",
130
      Authorization: "Zoho-oauthtoken " + auth.token,
131
    },
132
    body: JSON.stringify(body),
133
  });
134
  if (!response.ok) {
135
    const text = await response.text();
136
    throw new Error(`${response.status} ${text}`);
137
  }
138
  return await response.json();
139
}
140