Updating an Invoice Line Item's Discount Account via API Call / Deluge Custom Function
I need help updating an invoice line item's discount account via API. Below is a screenshot of the line item field I am referring to.
Now the field to the left of the highlighted field (discount account) is the sales income account. I am able to modify the sales income account via API completely fine.
I have a custom field called "Sales Account" with four options: Direct, Retail, Event, House Account, and Subscription. This field is in the invoices module.
This workflow has no criteria and immediately triggers the custom function "update_income_accounts," which fetches the invoice's "Sales Account" field value and runs through a series of if/else statements to determine the correct account ID for the sub-accounts nested under the main chart of accounts sales income account.
We're trying to automate updating the sales account association for all invoice line items by running this custom function.
So far, it works perfectly, except for the line item discount account. I can't get the invoice to reflect any changes to the discount account.
Below is the Zoho Books Custom Function I am using that works to update the Sales Income account but fails to reflect any change to the Discount account
- sales_account_value = "";
- account_id = "";
- discount_id = "";
- for each custom_field in invoice.get("custom_fields")
- {
- if(custom_field.get("field_id") == "4366441000017151041")
- {
- sales_account_value = custom_field.get("value_formatted");
- }
- }
- if(sales_account_value == "Direct")
- {
- account_id = "4366441000017139067";
- discount_id = "4366441000036176472";
- }
- else if(sales_account_value == "Retail")
- {
- account_id = "4366441000017139058";
- discount_id = "4366441000036176510";
- }
- else if(sales_account_value == "Event")
- {
- account_id = "4366441000017134275";
- discount_id = "4366441000036176506";
- }
- else if(sales_account_value == "House Account")
- {
- account_id = "4366441000036176498";
- discount_id = "4366441000036176502";
- }
- else if(sales_account_value == "Subscription")
- {
- account_id = "4366441000041000009";
- discount_id = "4366441000041000015";
- }
- invoice_ID = invoice.get("invoice_id");
- line_items = Map();
- url = "https://www.zohoapis.com/books/v3/invoices/" + invoice_ID + "?organization_id=HIDDEN_FOR_HELP";
- invoice_details = invokeurl
- [
- url :url
- type :GET
- connection:"HIDDEN_FOR_HELP"
- ];
- line_items = invoice_details.get("invoice").get("line_items");
- item_to_update = List();
- for each item in line_items
- {
- line_item_map = Map();
- line_item_map.put("line_item_id",item.get("line_item_id"));
- line_item_map.put("account_id",account_id);
- // set discount account via the discounts array
- discount_details = item.get("discounts");
- info discount_details;
- discounts_list = List();
- one_discount = Map();
- one_discount.put("discount_order",discount_details.get(0).get("discount_order"));
- one_discount.put("discount_amount",discount_details.get(0).get("discount_amount"));
- one_discount.put("discount_percent",discount_details.get(0).get("discount_percent"));
- one_discount.put("discount_account",discount_id);
- discounts_list.add(one_discount);
- line_item_map.put("discount_account_id",discount_id);
- line_item_map.put("discounts",discounts_list);
- item_to_update.add(line_item_map);
- info discounts_list;
- }
- info item_to_update;
- update_payload = Map();
- update_payload.put("line_items",item_to_update);
- update_invoice = invokeurl
- [
- url :url
- type :PUT
- parameters:update_payload.toString()
- connection:"HIDDEN_FOR_HELP"
- ];