ZohoAnalytics & ZohoBooks: Custom Related List from Analytics

ZohoAnalytics & ZohoBooks: Custom Related List from Analytics

What?
A quick article to document 2 features in deluge code: a custom related list in ZohoBooks, and a reminder on how to read a table from ZohoAnalytics.

Why?
My use-case here is that we have a client who uses their purchase orders and sales orders as part of a logistics solution where items are purchased from a supplier, sent to another supplier for grading, and then sent on to the end user/customer.

A custom field against the item record has been added which is a lookup to the Sales Order module. This means that on a purchase order, and per line item, the staff can specify which Sales Order the item relates to.

How?
At time of print, adding the lookup to the line item will automatically display a related list on the Sales Order but does not associate any records... not sure why this is. So we have a workaround, by sending all the data including the custom fields per line items to ZohoAnalytics; and in ZohoAnalytics importing a table based on a query which joins the sales orders and purchase order items by the custom lookup field.

What follows is the code required to build the custom related list in ZohoBooks:
copyraw
/* *******************************************************************************
Function:       Map Related_Purchase_Orders( Map salesorder , Map organization , Map user , Map page_context )
Label:          Related Purchase Orders
Trigger:        Displays on Sales Order record
Purpose:		To display related POs linked by a custom Sales Order lookup field on the Purchase Order line item.
Inputs:         salesorder
Outputs:        Related List

Date Created:   2024-07-09 (Joel Lipman)
                - Initial release
Date Modified:	???
                - ???
				
More Information:
                https://www.zoho.com/analytics/api/v2/bulk-api/export-data.html

******************************************************************************* */
//
// initialize
m_RelatedList = Map();
v_BooksOrgID = organization.get("organization_id");
v_SalesOrderID = salesorder.get("salesorder_id");
//
// analytics specific table to read data from (enter your own here)
v_ZA_WorkspaceID = "123456000000789012";
v_ZA_ViewID = "123467000009876543";
v_ZA_OrgID = "20240709007";
//
// build up request to send
m_Header = Map();
m_Header.put("ZANALYTICS-ORGID",v_ZA_OrgID);
m_Params = Map();
m_Params.put("responseFormat","json");
// note how criteria column is denoted by double-quotes and a value is by single-quotes.
m_Params.put("criteria","\"SO ID\"='" + v_SalesOrderID + "'");
m_Config = Map();
// sometimes parameters need to be a string... but this is working
m_Config.put("CONFIG",m_Params);
v_Endpoint = "https://analyticsapi.zoho.eu/restapi/v2/workspaces/" + v_ZA_WorkspaceID + "/views/" + v_ZA_ViewID + "/data";
//
// send request
r_PurchaseOrders = invokeurl
[
	url :v_Endpoint
	type :GET
	parameters:m_Config
	headers:m_Header
	connection:"my_analytics_connection"
];
//
// parse response
l_DataRows = ifnull(r_PurchaseOrders.get("data"),List());
//
// build up related list header row
l_HeaderColumns = List();
l_HeaderColumns.add({"key":"po_date","value":"Date"});
l_HeaderColumns.add({"key":"po_ref","value":"Ref"});
l_HeaderColumns.add({"key":"po_status","value":"Status"});
// amount/total is a currency column we want aligned on the right
l_HeaderColumns.add({"key":"po_amount","value":"PO Total","align":"right"});
l_HeaderColumns.add({"key":"last_modified","value":"Last Modified"});
//
// build up related list rows
l_RelatedListData = List();
for each  m_DataRow in l_DataRows
{
	// double-check in case books has returned 1st page rather than filtered rows
	if(m_DataRow.get("SO ID") == v_SalesOrderID)
	{
		m_Details = Map();
		m_Details.put("po_date",m_DataRow.get("PO Date").toDate().toString("dd-MMM-yyyy"));
		m_Details.put("po_ref",{"value":m_DataRow.get("PO Ref"),"isExternal":true,"link":"https://books.zoho.eu/app/" + v_BooksOrgID + "#/purchaseorders/" + m_DataRow.get("PO ID")});
		m_Details.put("po_status",m_DataRow.get("PO Status"));
		m_Details.put("po_amount",m_DataRow.get("PO Total"));
		m_Details.put("last_modified",m_DataRow.get("PO Modified"));
		l_RelatedListData.add(m_Details);
	}
}
//
// output related list
m_RelatedList = Map();
m_RelatedList.put("header_context",l_HeaderColumns);
m_RelatedList.put("data",l_RelatedListData);
return m_RelatedList;
  1.  /* ******************************************************************************* 
  2.  Function:       Map Related_Purchase_Orders( Map salesorder , Map organization , Map user , Map page_context ) 
  3.  Label:          Related Purchase Orders 
  4.  Trigger:        Displays on Sales Order record 
  5.  Purpose:        To display related POs linked by a custom Sales Order lookup field on the Purchase Order line item. 
  6.  Inputs:         salesorder 
  7.  Outputs:        Related List 
  8.   
  9.  Date Created:   2024-07-09 (Joel Lipman) 
  10.                  - Initial release 
  11.  Date Modified:    ??? 
  12.                  - ??? 
  13.   
  14.  More Information: 
  15.                  https://www.zoho.com/analytics/api/v2/bulk-api/export-data.html 
  16.   
  17.  ******************************************************************************* */ 
  18.  // 
  19.  // initialize 
  20.  m_RelatedList = Map()
  21.  v_BooksOrgID = organization.get("organization_id")
  22.  v_SalesOrderID = salesorder.get("salesorder_id")
  23.  // 
  24.  // analytics specific table to read data from (enter your own here) 
  25.  v_ZA_WorkspaceID = "123456000000789012"
  26.  v_ZA_ViewID = "123467000009876543"
  27.  v_ZA_OrgID = "20240709007"
  28.  // 
  29.  // build up request to send 
  30.  m_Header = Map()
  31.  m_Header.put("ZANALYTICS-ORGID",v_ZA_OrgID)
  32.  m_Params = Map()
  33.  m_Params.put("responseFormat","json")
  34.  // note how criteria column is denoted by double-quotes and a value is by single-quotes. 
  35.  m_Params.put("criteria","\"SO ID\"='" + v_SalesOrderID + "'")
  36.  m_Config = Map()
  37.  // sometimes parameters need to be a string... but this is working 
  38.  m_Config.put("CONFIG",m_Params)
  39.  v_Endpoint = "https://analyticsapi.zoho.eu/restapi/v2/workspaces/" + v_ZA_WorkspaceID + "/views/" + v_ZA_ViewID + "/data"
  40.  // 
  41.  // send request 
  42.  r_PurchaseOrders = invokeUrl 
  43.  [ 
  44.      url :v_Endpoint 
  45.      type :GET 
  46.      parameters:m_Config 
  47.      headers:m_Header 
  48.      connection:"my_analytics_connection" 
  49.  ]
  50.  // 
  51.  // parse response 
  52.  l_DataRows = ifnull(r_PurchaseOrders.get("data"),List())
  53.  // 
  54.  // build up related list header row 
  55.  l_HeaderColumns = List()
  56.  l_HeaderColumns.add({"key":"po_date","value":"Date"})
  57.  l_HeaderColumns.add({"key":"po_ref","value":"Ref"})
  58.  l_HeaderColumns.add({"key":"po_status","value":"Status"})
  59.  // amount/total is a currency column we want aligned on the right 
  60.  l_HeaderColumns.add({"key":"po_amount","value":"PO Total","align":"right"})
  61.  l_HeaderColumns.add({"key":"last_modified","value":"Last Modified"})
  62.  // 
  63.  // build up related list rows 
  64.  l_RelatedListData = List()
  65.  for each  m_DataRow in l_DataRows 
  66.  { 
  67.      // double-check in case books has returned 1st page rather than filtered rows 
  68.      if(m_DataRow.get("SO ID") == v_SalesOrderID) 
  69.      { 
  70.          m_Details = Map()
  71.          m_Details.put("po_date",m_DataRow.get("PO Date").toDate().toString("dd-MMM-yyyy"))
  72.          m_Details.put("po_ref",{"value":m_DataRow.get("PO Ref"),"isExternal":true,"link":"https://books.zoho.eu/app/" + v_BooksOrgID + "#/purchaseorders/" + m_DataRow.get("PO ID")})
  73.          m_Details.put("po_status",m_DataRow.get("PO Status"))
  74.          m_Details.put("po_amount",m_DataRow.get("PO Total"))
  75.          m_Details.put("last_modified",m_DataRow.get("PO Modified"))
  76.          l_RelatedListData.add(m_Details)
  77.      } 
  78.  } 
  79.  // 
  80.  // output related list 
  81.  m_RelatedList = Map()
  82.  m_RelatedList.put("header_context",l_HeaderColumns)
  83.  m_RelatedList.put("data",l_RelatedListData)
  84.  return m_RelatedList; 

Additional
  • Note that for reading the data from Analytics, we cheat by creating a table in ZohoAnalytics rather than simply writing a query and using the 'asynchronous' method to extract the query result. To create an analytics table from a query, we do the following:
    1. Login to Zoho Analytics, and choose a workspace with the relevant data (in this case Zoho Books Analytics workspace)
    2. In the left sidebar, select "Data Sources" then click on the "Add Data Sources" button
    3. Select "Zoho Analytics Workspace" as the data source type.
    4. Select the relevant workspace and click on "Next"
    5. Select "Custom Query" in step 2 and paste your Zoho SQL query in here.
    6. Give it a name, set the schedule and then wait for it to finish building the table.

  • If you want the Zoho SQL (Ansi-SQL?) used to link the purchase order items with the sales order items based on the custom lookup field:
    copyraw
    SELECT
    		 po."Purchase Order ID" AS "PO ID",
    		 po."Purchase Order Number" AS "PO Ref",
    		 po."Purchase Order Date" AS "PO Date",
    		 po."Purchase Order Status" AS "PO Status",
    		 poi."Item Name" AS "PO Item",
    		 poi."Item Price (BCY)" AS "PO Item Price",
    		 poi."Graded Score" AS "PO Item Grade",
    		 poi."Quantity" AS "PO Item Qty",
    		 poi."Quantity Billed" AS "PO Item Qty Billed",
    		 poi."Quantity Received" AS "PO Item Qty Rcved",
    		 po."Total (BCY)" AS "PO Total",
    		 po."Last Modified Time" AS "PO Modified",
    		 so."Sales order ID" AS "SO ID",
    		 so."Sales Order#" AS "SO Ref",
    		 so."Order Date" AS "SO Date",
    		 so."Status" AS "SO Status",
    		 soi."Item Name" AS "SO Item",
    		 soi."Item Price (BCY)" AS "SO Item Price",
    		 soi."Quantity" AS "SO Item Qty",
    		 soi."Quantity Packed" AS "SO Item Qty Packed",
    		 soi."Quantity Shipped" AS "SO Item Qty Shipped",
    		 soi."Quantity Delivered" AS "SO Item Qty Delivered",
    		 soi."Graded Score" AS "SO Item Grade",
    		 soi."Confirmed" AS "SO Item Confirmed",
    		 so."Last Modified Time" AS "SO Modified"
    FROM  "Purchase Orders" po
    LEFT JOIN "Purchase Order Items" poi ON po."Purchase Order ID"  = poi."Purchase Order ID" 
    LEFT JOIN "Sales Orders" so ON poi."Linked SO"  = so."Sales order ID" 
    LEFT JOIN "Sales Order Items" soi ON so."Sales order ID"  = soi."Sales order ID"
    1.  SELECT 
    2.           po."Purchase Order ID" AS "PO ID", 
    3.           po."Purchase Order Number" AS "PO Ref", 
    4.           po."Purchase Order Date" AS "PO Date", 
    5.           po."Purchase Order Status" AS "PO Status", 
    6.           poi."Item Name" AS "PO Item", 
    7.           poi."Item Price (BCY)" AS "PO Item Price", 
    8.           poi."Graded Score" AS "PO Item Grade", 
    9.           poi."Quantity" AS "PO Item Qty", 
    10.           poi."Quantity Billed" AS "PO Item Qty Billed", 
    11.           poi."Quantity Received" AS "PO Item Qty Rcved", 
    12.           po."Total (BCY)" AS "PO Total", 
    13.           po."Last Modified Time" AS "PO Modified", 
    14.           so."Sales order ID" AS "SO ID", 
    15.           so."Sales Order#" AS "SO Ref", 
    16.           so."Order Date" AS "SO Date", 
    17.           so."Status" AS "SO Status", 
    18.           soi."Item Name" AS "SO Item", 
    19.           soi."Item Price (BCY)" AS "SO Item Price", 
    20.           soi."Quantity" AS "SO Item Qty", 
    21.           soi."Quantity Packed" AS "SO Item Qty Packed", 
    22.           soi."Quantity Shipped" AS "SO Item Qty Shipped", 
    23.           soi."Quantity Delivered" AS "SO Item Qty Delivered", 
    24.           soi."Graded Score" AS "SO Item Grade", 
    25.           soi."Confirmed" AS "SO Item Confirmed", 
    26.           so."Last Modified Time" AS "SO Modified" 
    27.  FROM  "Purchase Orders" po 
    28.  LEFT JOIN "Purchase Order Items" poi ON po."Purchase Order ID"  = poi."Purchase Order ID" 
    29.  LEFT JOIN "Sales Orders" so ON poi."Linked SO"  = so."Sales order ID" 
    30.  LEFT JOIN "Sales Order Items" soi ON so."Sales order ID"  = soi."Sales order ID" 

  • Initially we thought we could take the purchase order by simply going through the comments but this only shows Purchase Orders raised from a Sales Order which was not accurate enough... but here's the snippet:
    copyraw
    r_CommentsHistory = Map();
    l_Comments = ifnull(r_CommentsHistory.get("comments"),List());
    for each  m_Comment in l_Comments
    {
    	if(m_Comment.get("description").containsIgnoreCase("Purchase order"))
    	{
    		v_PO_Ref = "PO-" + m_Comment.get("description").getSuffix("PO-").trim().getPrefix(" ");
    		m_Details = Map();
    		m_Details.put("po_ref",{"value":v_PO_Ref,"isExternal":true,"link":"https://books.zoho.com/app#/purchaseorders/" + v_PO_Ref});
    		m_Details.put("po_date",zoho.currentdate.toString("dd-MMM-yyyy"));
    		m_Details.put("last_modified",zoho.currentdate.toString("dd-MMM-yyyy HH:mm:ss"));
    		l_Data.add(m_Details);
    	}
    }
    1.  r_CommentsHistory = Map()
    2.  l_Comments = ifnull(r_CommentsHistory.get("comments"),List())
    3.  for each  m_Comment in l_Comments 
    4.  { 
    5.      if(m_Comment.get("description").containsIgnoreCase("Purchase order")) 
    6.      { 
    7.          v_PO_Ref = "PO-" + m_Comment.get("description").getSuffix("PO-").trim().getPrefix(" ")
    8.          m_Details = Map()
    9.          m_Details.put("po_ref",{"value":v_PO_Ref,"isExternal":true,"link":"https://books.zoho.com/app#/purchaseorders/" + v_PO_Ref})
    10.          m_Details.put("po_date",zoho.currentdate.toString("dd-MMM-yyyy"))
    11.          m_Details.put("last_modified",zoho.currentdate.toString("dd-MMM-yyyy HH:mm:ss"))
    12.          l_Data.add(m_Details)
    13.      } 
    14.  } 

Source(s):
Category: Zoho :: Article: 877

Please publish modules in offcanvas position.