Edits history of script submission #21987 for ' Update a contact using a custom field's unique value (zoho)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    //native
    type Zoho = {
      token: string;
    };
    /**
     * Update a contact using a custom field's unique value
     * A custom field will have unique values if it's configured to not accept duplicate values.
     */
    export async function main(
      auth: Zoho,
      organization_id: string | undefined,
      X_Unique_Identifier_Key: string,
      X_Unique_Identifier_Value: string,
      body: {
        contact_name: string;
        company_name?: string;
        payment_terms?: number;
        payment_terms_label?: string;
        contact_type?: string;
        customer_sub_type?: string;
        currency_id?: string;
        opening_balances?: {
          location_id?: string;
          exchange_rate?: number;
          opening_balance_amount?: number;
        }[];
        credit_limit?: number;
        tags?: { tag_id?: string; tag_option_id?: string }[];
        website?: string;
        owner_id?: string;
        custom_fields?: { index?: number; value?: string; label?: string }[];
        billing_address?: {
          attention?: string;
          address?: string;
          street2?: string;
          state_code?: string;
          city?: string;
          state?: string;
          zip?: string;
          country?: string;
          fax?: string;
          phone?: string;
        };
        shipping_address?: {
          attention?: string;
          address?: string;
          street2?: string;
          state_code?: string;
          city?: string;
          state?: string;
          zip?: string;
          country?: string;
          fax?: string;
          phone?: string;
        };
        contact_persons?: {
          contact_person_id?: string;
          salutation?: string;
          first_name?: string;
          last_name?: string;
          email?: string;
          phone?: string;
          mobile?: string;
          designation?: string;
          department?: string;
          skype?: string;
          is_primary_contact?: false | true;
          enable_portal?: false | true;
        }[];
        default_templates?: {
          invoice_template_id?: string;
          estimate_template_id?: string;
          creditnote_template_id?: string;
          purchaseorder_template_id?: string;
          salesorder_template_id?: string;
          retainerinvoice_template_id?: string;
          paymentthankyou_template_id?: string;
          retainerinvoice_paymentthankyou_template_id?: string;
          invoice_email_template_id?: string;
          estimate_email_template_id?: string;
          creditnote_email_template_id?: string;
          purchaseorder_email_template_id?: string;
          salesorder_email_template_id?: string;
          retainerinvoice_email_template_id?: string;
          paymentthankyou_email_template_id?: string;
          retainerinvoice_paymentthankyou_email_template_id?: string;
        };
        notes?: string;
        vat_reg_no?: string;
        tax_reg_no?: string;
        country_code?: string;
        tax_treatment?: string;
        tax_exemption_certificate_number?: string;
        tax_regime?: string;
        legal_name?: string;
        is_tds_registered?: false | true;
        vat_treatment?: string;
        place_of_contact?: string;
        gst_no?: string;
        gst_treatment?: string;
        tax_authority_name?: string;
        avatax_exempt_no?: string;
        avatax_use_code?: string;
        tax_exemption_id?: string;
        tax_exemption_code?: string;
        tax_authority_id?: string;
        tax_id?: string;
        is_taxable?: false | true;
        facebook?: string;
        twitter?: string;
        track_1099?: false | true;
        tax_id_type?: string;
        tax_id_value?: string;
      },
      X_Upsert?: string,
    ) {
      const url = new URL(`https://www.zohoapis.com/books/v3/contacts`);
      for (const [k, v] of [["organization_id", organization_id]]) {
        if (v !== undefined && v !== "" && k !== undefined) {
          url.searchParams.append(k, v);
        }
      }
      const response = await fetch(url, {
        method: "PUT",
        headers: {
          "X-Unique-Identifier-Key": X_Unique_Identifier_Key,
          "X-Unique-Identifier-Value": X_Unique_Identifier_Value,
          ...(X_Upsert ? { "X-Upsert": X_Upsert } : {}),
          "Content-Type": "application/json",
          Authorization: "Zoho-oauthtoken " + auth.token,
        },
        body: JSON.stringify(body),
      });
      if (!response.ok) {
        const text = await response.text();
        throw new Error(`${response.status} ${text}`);
      }
      return await response.json();
    }
    

    Submitted by hugo697 235 days ago