Edits history of script submission #21984 for ' Update a bill 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 bill 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: {
        vendor_id: string;
        currency_id?: string;
        vat_treatment?: string;
        is_update_customer?: false | true;
        purchaseorder_ids?: string[];
        bill_number: string;
        documents?: { document_id?: number; file_name?: string }[];
        source_of_supply?: string;
        destination_of_supply?: string;
        place_of_supply?: string;
        permit_number?: string;
        gst_treatment?: string;
        tax_treatment?: string;
        gst_no?: string;
        pricebook_id?: string;
        reference_number?: string;
        date?: string;
        due_date?: string;
        payment_terms?: number;
        payment_terms_label?: string;
        recurring_bill_id?: string;
        exchange_rate?: number;
        is_item_level_tax_calc?: false | true;
        is_inclusive_tax?: false | true;
        adjustment?: number;
        adjustment_description?: string;
        custom_fields?: { index?: number; value?: string }[];
        line_items?: {
          purchaseorder_item_id?: string;
          line_item_id?: string;
          item_id?: string;
          name?: string;
          account_id?: string;
          description?: string;
          rate?: number;
          hsn_or_sac?: string;
          reverse_charge_tax_id?: string;
          location_id?: string;
          quantity?: number;
          tax_id?: string;
          tds_tax_id?: string;
          tax_treatment_code?: string;
          tax_exemption_id?: string;
          tax_exemption_code?: string;
          item_order?: number;
          product_type?: string;
          acquisition_vat_id?: string;
          reverse_charge_vat_id?: string;
          unit?: string;
          tags?: { tag_id?: number; tag_option_id?: number }[];
          is_billable?: false | true;
          project_id?: string;
          customer_id?: string;
          item_custom_fields?: {
            custom_field_id?: number;
            index?: number;
            value?: string;
            label?: string;
          }[];
          serial_numbers?: string[];
        }[];
        location_id?: string;
        taxes?: { tax_id?: string; tax_name?: string; tax_amount?: string }[];
        notes?: string;
        terms?: string;
        approvers?: { approver_id?: number; order?: number }[];
      },
      X_Upsert?: string,
    ) {
      const url = new URL(`https://www.zohoapis.com/books/v3/bills`);
      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