A collection of code snippets I seem to be regularly using to generate a dynamic map of system values held in a ZohoBooks instance.
Why?
Rather than hard-coding and having a ton of if..then statements, I can feed these maps a textual value and it returns the ID to use. Some of these can be found elsewhere in my site but I'm putting all of them here just for quick reference.
How?
Note that for the below, I recently updated this article (2024-05-21) due to the API domain name change from https://books.zoho.com to https://www.zohoapis.com/books and changed the Top Level Domain (or Zoho DataCenter) from EU to COM as I was using this for a client on the US datacenter. I have a connection called "zbooks" and the v_BooksOrgID is the ID of the instance in ZohoBooks; so for multiple instances of ZohoBooks for the same organization, a separate call would need to be made with a different variable org ID.
/* ******************************************************************************* Function: Map fn_GetSystemValues() Trigger: Snippet to be used at beginning of code to get dynamic maps of system values Purpose: Taxes, Currencies, Payment Terms, Sales Persons, and Nominal Accounts Inputs: - Outputs: - Date Created: 2021-09-24 (oel Lipman) - Initial release Date Modified: 2024-05-21 (Joel Lipman) - Change of API domain from https://books.zoho.com to https://www.zohoapis.com/books ******************************************************************************* */ // // our made up books org id but enter your own v_BooksOrgID=12345678901; // // --------------------------------- // ZohoBooks Taxes m_Taxes = Map(); r_Taxes = invokeurl [ url :"https://www.zohoapis.com/books/v3/settings/taxes?organization_id=" + v_BooksOrgID type :GET connection:"zbooks" ]; for each m_Tax in r_Taxes.get("taxes") { m_Taxes.put(m_Tax.get("tax_percentage").toString(),m_Tax.get("tax_id")); } info m_Taxes; // // --------------------------------- // get currencies m_Currencies = Map(); r_Currencies = invokeUrl [ url :"https://www.zohoapis.com/books/v3/settings/currencies?organization_id=" + v_BooksOrgID type :GET connection:"zbooks" ]; for each m_Currency in r_Currencies.get("currencies") { m_Currencies.put(m_Currency.get("currency_code"),m_Currency.get("currency_id")); } info m_Currencies; // // --------------------------------- // get payment terms m_PaymentTerms = Map(); r_PaymentTerms = invokeUrl [ url :"https://www.zohoapis.com/books/v3/settings/paymentterms?organization_id=" + v_BooksOrgID type :GET connection:"zbooks" ]; l_PaymentTerms = r_PaymentTerms.get("data").get("payment_terms"); for each m_PaymentTerm in l_PaymentTerms { m_PaymentTerms.put(m_PaymentTerm.get("payment_terms_label"),m_PaymentTerm.get("payment_terms")); } info m_PaymentTerms; // // --------------------------------- // get sales persons m_SalesPersons = Map(); /* r_SoDetails = zoho.crm.getRecordById("Sales_Orders",p_SoID); v_CRMOwnerID = r_SoDetails.get("Owner").get("id"); v_Filter = "crm_reference_id=" + v_OwnerID; */ // set v_Filter to blank to retrieve all sales persons v_Filter = ""; r_AllSalesPersons = zoho.books.getRecords("salespersons", v_BooksOrgID, v_Filter, "zbooks"); for each m_SalesPerson in r_AllSalesPersons.get("data") { m_SalesPersons.put(m_SalesPerson.get("salesperson_email"),m_SalesPerson.get("salesperson_id")); } info m_SalesPersons; // // --------------------------------- // get chart of accounts to use (both sales and purchases) r_ChartOfAccounts = invokeurl [ url :"https://www.zohoapis.com/books/v3/chartofaccounts?organization_id=" + v_BooksOrgID type :GET connection:"zbooks" ]; m_NominalAccounts = Map(); if(!isnull(r_ChartOfAccounts.get("chartofaccounts"))) { for each m_NomAccount in r_ChartOfAccounts.get("chartofaccounts") { m_NominalAccounts.put(m_NomAccount.get("account_name"),m_NomAccount.get("account_id")); } } info m_NominalAccounts; / // --------------------------------- // get delivery methods r_DeliveryMethods = invokeurl [ url :"https://www.zohoapis.com/books/v3/deliverymethods?organization_id=" + v_BooksOrgID type :GET connection:"zbooks" ]; m_DeliveryMethods = Map(); if(!isnull(r_DeliveryMethods.get("data"))) { if(!isnull(r_DeliveryMethods.get("data").get("delivery_methods"))) { l_DeliveryMethods = r_DeliveryMethods.get("data").get("delivery_methods"); for each m_DeliveryMethod in l_DeliveryMethods { m_DeliveryMethods.put(m_DeliveryMethod.get("delivery_method"),m_DeliveryMethod.get("delivery_method_id")); } } } info m_DeliveryMethods;
- /* *******************************************************************************
- Function: Map fn_GetSystemValues()
- Trigger: Snippet to be used at beginning of code to get dynamic maps of system values
- Purpose: Taxes, Currencies, Payment Terms, Sales Persons, and Nominal Accounts
- Inputs: -
- Outputs: -
- Date Created: 2021-09-24 (oel Lipman)
- - Initial release
- Date Modified: 2024-05-21 (Joel Lipman)
- - Change of API domain from https://books.zoho.com to https://www.zohoapis.com/books
- ******************************************************************************* */
- //
- // our made up books org id but enter your own
- v_BooksOrgID=12345678901;
- //
- // ---------------------------------
- // ZohoBooks Taxes
- m_Taxes = Map();
- r_Taxes = invokeUrl
- [
- url :"https://www.zohoapis.com/books/v3/settings/taxes?organization_id=" + v_BooksOrgID
- type :GET
- connection:"zbooks"
- ];
- for each m_Tax in r_Taxes.get("taxes")
- {
- m_Taxes.put(m_Tax.get("tax_percentage").toString(),m_Tax.get("tax_id"));
- }
- info m_Taxes;
- //
- // ---------------------------------
- // get currencies
- m_Currencies = Map();
- r_Currencies = invokeUrl
- [
- url :"https://www.zohoapis.com/books/v3/settings/currencies?organization_id=" + v_BooksOrgID
- type :GET
- connection:"zbooks"
- ];
- for each m_Currency in r_Currencies.get("currencies")
- {
- m_Currencies.put(m_Currency.get("currency_code"),m_Currency.get("currency_id"));
- }
- info m_Currencies;
- //
- // ---------------------------------
- // get payment terms
- m_PaymentTerms = Map();
- r_PaymentTerms = invokeUrl
- [
- url :"https://www.zohoapis.com/books/v3/settings/paymentterms?organization_id=" + v_BooksOrgID
- type :GET
- connection:"zbooks"
- ];
- l_PaymentTerms = r_PaymentTerms.get("data").get("payment_terms");
- for each m_PaymentTerm in l_PaymentTerms
- {
- m_PaymentTerms.put(m_PaymentTerm.get("payment_terms_label"),m_PaymentTerm.get("payment_terms"));
- }
- info m_PaymentTerms;
- //
- // ---------------------------------
- // get sales persons
- m_SalesPersons = Map();
- /*
- r_SoDetails = zoho.crm.getRecordById("Sales_Orders",p_SoID);
- v_CRMOwnerID = r_SoDetails.get("Owner").get("id");
- v_Filter = "crm_reference_id=" + v_OwnerID;
- */
- // set v_Filter to blank to retrieve all sales persons
- v_Filter = "";
- r_AllSalesPersons = zoho.books.getRecords("salespersons", v_BooksOrgID, v_Filter, "zbooks");
- for each m_SalesPerson in r_AllSalesPersons.get("data")
- {
- m_SalesPersons.put(m_SalesPerson.get("salesperson_email"),m_SalesPerson.get("salesperson_id"));
- }
- info m_SalesPersons;
- //
- // ---------------------------------
- // get chart of accounts to use (both sales and purchases)
- r_ChartOfAccounts = invokeUrl
- [
- url :"https://www.zohoapis.com/books/v3/chartofaccounts?organization_id=" + v_BooksOrgID
- type :GET
- connection:"zbooks"
- ];
- m_NominalAccounts = Map();
- if(!isnull(r_ChartOfAccounts.get("chartofaccounts")))
- {
- for each m_NomAccount in r_ChartOfAccounts.get("chartofaccounts")
- {
- m_NominalAccounts.put(m_NomAccount.get("account_name"),m_NomAccount.get("account_id"));
- }
- }
- info m_NominalAccounts;
- /
- // ---------------------------------
- // get delivery methods
- r_DeliveryMethods = invokeUrl
- [
- url :"https://www.zohoapis.com/books/v3/deliverymethods?organization_id=" + v_BooksOrgID
- type :GET
- connection:"zbooks"
- ];
- m_DeliveryMethods = Map();
- if(!isnull(r_DeliveryMethods.get("data")))
- {
- if(!isnull(r_DeliveryMethods.get("data").get("delivery_methods")))
- {
- l_DeliveryMethods = r_DeliveryMethods.get("data").get("delivery_methods");
- for each m_DeliveryMethod in l_DeliveryMethods
- {
- m_DeliveryMethods.put(m_DeliveryMethod.get("delivery_method"),m_DeliveryMethod.get("delivery_method_id"));
- }
- }
- }
- info m_DeliveryMethods;