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, the Top Level Domain (or Zoho DataCenter) is EU rather than the COM. 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 (Ascent Business - Joel Lipman) - Initial release Date Modified: ??? - ??? ******************************************************************************* */ // // our made up books org id but enter your own v_BooksOrgID=12345678901; // // --------------------------------- // ZohoBooks Taxes m_Taxes = Map(); r_Taxes = invokeurl [ url :"https://books.zoho.eu/api/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://books.zoho.eu/api/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://books.zoho.com/api/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: UK r_ChartOfAccountsUK = invokeurl [ url :"https://books.zoho.eu/api/v3/chartofaccounts?organization_id=" + v_BooksOrgID type :GET connection:"zbooks" ]; m_NominalAccountsUK = Map(); if(!isnull(r_ChartOfAccountsUK.get("chartofaccounts"))) { for each m_NomAccount in r_ChartOfAccountsUK.get("chartofaccounts") { m_NominalAccountsUK.put(m_NomAccount.get("account_name"),m_NomAccount.get("account_id")); } } info m_NominalAccountsUK;
- /* *******************************************************************************
- 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 (Ascent Business - Joel Lipman)
- - Initial release
- Date Modified: ???
- - ???
- ******************************************************************************* */
- //
- // our made up books org id but enter your own
- v_BooksOrgID=12345678901;
- //
- // ---------------------------------
- // ZohoBooks Taxes
- m_Taxes = Map();
- r_Taxes = invokeUrl
- [
- url :"https://books.zoho.eu/api/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://books.zoho.eu/api/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://books.zoho.com/api/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: UK
- r_ChartOfAccountsUK = invokeUrl
- [
- url :"https://books.zoho.eu/api/v3/chartofaccounts?organization_id=" + v_BooksOrgID
- type :GET
- connection:"zbooks"
- ];
- m_NominalAccountsUK = Map();
- if(!isnull(r_ChartOfAccountsUK.get("chartofaccounts")))
- {
- for each m_NomAccount in r_ChartOfAccountsUK.get("chartofaccounts")
- {
- m_NominalAccountsUK.put(m_NomAccount.get("account_name"),m_NomAccount.get("account_id"));
- }
- }
- info m_NominalAccountsUK;