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;
- /* *******************************************************************************
- 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;
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:
- Login to Zoho Analytics, and choose a workspace with the relevant data (in this case Zoho Books Analytics workspace)
- In the left sidebar, select "Data Sources" then click on the "Add Data Sources" button
- Select "Zoho Analytics Workspace" as the data source type.
- Select the relevant workspace and click on "Next"
- Select "Custom Query" in step 2 and paste your Zoho SQL query in here.
- 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"
- 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"
- 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); } }
- 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);
- }
- }
Source(s):
Category: Zoho :: Article: 877