Edits history of script submission #22031 for ' Update an expense 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 an expense 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: {
        account_id: string;
        date: string;
        amount: number;
        tax_id?: string;
        source_of_supply?: string;
        destination_of_supply?: string;
        place_of_supply?: string;
        hsn_or_sac?: string;
        gst_no?: string;
        reverse_charge_tax_id?: string;
        line_items?: {
          line_item_id?: string;
          account_id?: string;
          description?: string;
          amount?: number;
          tax_id?: string;
          item_order?: string;
          product_type?: string;
          acquisition_vat_id?: string;
          reverse_charge_vat_id?: string;
          reverse_charge_tax_id?: string;
          tax_exemption_code?: string;
          tax_exemption_id?: string;
          location_id?: string;
        }[];
        location_id?: string;
        taxes?: { tax_id?: string; tax_amount?: number }[];
        is_inclusive_tax?: false | true;
        is_billable?: false | true;
        reference_number?: string;
        description?: string;
        customer_id?: string;
        currency_id?: string;
        exchange_rate?: number;
        project_id?: string;
        mileage_type?: string;
        vat_treatment?: string;
        tax_treatment?: string;
        product_type?: string;
        acquisition_vat_id?: string;
        reverse_charge_vat_id?: string;
        start_reading?: number;
        end_reading?: number;
        distance?: string;
        mileage_unit?: string;
        mileage_rate?: number;
        employee_id?: string;
        vehicle_type?: string;
        can_reclaim_vat_on_mileage?: string;
        fuel_type?: string;
        engine_capacity_range?: string;
        paid_through_account_id: string;
        vendor_id?: string;
        custom_fields?: string[];
      },
      X_Upsert?: string,
    ) {
      const url = new URL(`https://www.zohoapis.com/books/v3/expenses`);
      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