Print

Zoho CRM & Zoho Books: Custom Related Lists Deluge

What?
This is an article to quickly demo a couple of snippets of code to display values in a custom related list as well as to display empty custom related lists.

Why?
Because I keep forgetting how to do this and it takes about an hour to go through the documentation and get a working solution.

How?
So the article below shows how to do this Zoho CRM and how to do it in Zoho Books...

The example below is that we are going to search for some records in Zoho Creator and display these in either Zoho Books or Zoho CRM. For demonstration purposes, these have been simplified and will need to be adapted for your needs. Note that I have used 2 URL methods of linking to the Creator app and some data formatting that I tend to use regularly for my benefit later on when I simply copy & paste the below code templates.

Zoho CRM
copyraw
//
// initialize
v_Index = 0;
//
// search in Creator
v_OwnerName = <AppOwner>;
v_AppLinkName = <AppName>;
v_FormLinkName = <FormName>;
v_ViewLinkName = <ReportName>;
v_Condition = "(Opportunities==\"" + v_DealID + "\")&&(UpdateClick==\"Yes\")";
r_SearchResults = zoho.creator.getRecords(v_OwnerName,v_AppLinkName,v_ViewLinkName,v_Condition,1,100,"joels_connection");
v_ResponseCode = ifnull(r_SearchResults.get("code"),500).toLong();
//
// if found records
if(v_ResponseCode == 3000)
{
	v_RelatedListXML = "<record>";
	l_SearchResults = r_SearchResults.get("data").toJSONList();
	for each  r_Result in l_SearchResults
	{
		// init
		v_DateTill= "-";
		v_QuoteTotalDisp= "-";
		v_Index = v_Index + 1;
		v_RelatedListXML = v_RelatedListXML + "<row no=\"" + v_Index + "\">";
		//
		// retrieve
		v_CreatorQuoteID = r_Result.get("ID");
		v_LinkUrl2 = "https://creatorapp.zoho.eu/" + v_OwnerName + "/" + v_AppLinkName + "/#Page:OpenQuote?ID=" + v_CreatorQuoteID;
		v_QuoteRef = ifnull(r_Result.get("Quote_Ref"),"-");
		v_QuoteName = ifnull(r_Result.get("Quote_Name"),"-");
		v_QuoteStage = ifnull(r_Result.get("Quote_Stage"),"-");
		v_QuoteCurr = ifnull(r_Result.get("Quote_Currency"),"GBP");
		v_QuoteTotal = ifnull(r_Result.get("Total_Amount"),0.0).toDecimal();
		v_ExchangeRate = ifnull(r_Result.get("Exchange_Rate"),1);
		v_ModifiedTime = ifnull(r_Result.get("Modified_Time"),r_Result.get("Added_Time")).toTime().toString("dd/MM/yyyy hh:mm a");
		//
		// future proofs
		v_ExchangeRate = if(v_ExchangeRate == 0,1,v_ExchangeRate);
		// 
		// transforms
		if(r_Result.get("Valid_Till") != null)
		{
			v_DateTill = r_Result.get("Valid_Until").toDate().toString("dd/MM/yyyy");
		}
		if(v_QuoteTotal != 0)
		{
			v_QuoteTotalGBP = (v_QuoteTotal.toDecimal() / v_ExchangeRate).toDecimal().round(2);
			//
			// format currency with commas (thousand separator) and 2 decimals
			v_QuoteTotalStr = v_QuoteTotal.toDecimal().round(2).toString().replaceAll("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)",",");
			v_QuoteTotalGBPStr = v_QuoteTotalGBP.toDecimal().round(2).toString().replaceAll("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)",",");
			//
			// to display both side by side if different otherwise display GBP
			if(!isnull(m_Currencies.get(v_QuoteCurr)))
			{
				if(v_QuoteCurr == "GBP")
				{
					v_QuoteTotalDisp = m_Currencies.get(v_QuoteCurr) + " " + v_QuoteTotalGBPStr;
				}
				else
				{
					v_QuoteTotalDisp = m_Currencies.get(v_QuoteCurr) + " " + v_QuoteTotalStr + " (£ " + v_QuoteTotalGBPStr + ")";
				}
			}
			else
			{
				v_QuoteTotalDisp = v_QuoteTotalStr + " " + v_QuoteCurr + " (£ " + v_QuoteTotalGBPStr + ")";
			}
		}
		v_RelatedListXML = v_RelatedListXML + "<FL val=\"Quote Ref\">" + v_QuoteRef + "</FL>";
		// don't forget to escape quote name with CDATA in case of XML banned characters
		v_RelatedListXML = v_RelatedListXML + "<FL val=\"Quote Name\" link=\"true\" url=\"" + v_LinkUrl2 + "\"><![CDATA[" + v_QuoteName + "]]></FL>";
		v_RelatedListXML = v_RelatedListXML + "<FL val=\"Valid Till\">" + v_DateTill + "</FL>";
		v_RelatedListXML = v_RelatedListXML + "<FL val=\"Stage\">" + v_QuoteStage + "</FL>";
		v_RelatedListXML = v_RelatedListXML + "<FL val=\"Total Amount\">" + v_QuoteTotalDisp + "</FL>";
		v_RelatedListXML = v_RelatedListXML + "<FL val=\"Modified Time\">" + v_ModifiedTime + "</FL>";
		v_RelatedListXML = v_RelatedListXML + "</row>";
		v_Index = v_Index + 1;
	}
	v_RelatedListXML = v_RelatedListXML + "</record>";
}
else
{
	// display "no records found"
	v_RelatedListXML = "<error><message>";
	v_RelatedListXML = v_RelatedListXML + "No records found";
	v_RelatedListXML = v_RelatedListXML + "</message></error>";
}
//
// output
return v_RelatedListXML;
  1.  // 
  2.  // initialize 
  3.  v_Index = 0
  4.  // 
  5.  // search in Creator 
  6.  v_OwnerName = <AppOwner>
  7.  v_AppLinkName = <AppName>
  8.  v_FormLinkName = <FormName>
  9.  v_ViewLinkName = <ReportName>
  10.  v_Condition = "(Opportunities==\"" + v_DealID + "\")&&(UpdateClick==\"Yes\")"
  11.  r_SearchResults = zoho.creator.getRecords(v_OwnerName,v_AppLinkName,v_ViewLinkName,v_Condition,1,100,"joels_connection")
  12.  v_ResponseCode = ifnull(r_SearchResults.get("code"),500).toLong()
  13.  // 
  14.  // if found records 
  15.  if(v_ResponseCode == 3000) 
  16.  { 
  17.      v_RelatedListXML = "<record>"
  18.      l_SearchResults = r_SearchResults.get("data").toJSONList()
  19.      for each  r_Result in l_SearchResults 
  20.      { 
  21.          // init 
  22.          v_DateTill= "-"
  23.          v_QuoteTotalDisp= "-"
  24.          v_Index = v_Index + 1
  25.          v_RelatedListXML = v_RelatedListXML + "<row no=\"" + v_Index + "\">"
  26.          // 
  27.          // retrieve 
  28.          v_CreatorQuoteID = r_Result.get("ID")
  29.          v_LinkUrl2 = "https://creatorapp.zoho.eu/" + v_OwnerName + "/" + v_AppLinkName + "/#Page:OpenQuote?ID=" + v_CreatorQuoteID; 
  30.          v_QuoteRef = ifnull(r_Result.get("Quote_Ref"),"-")
  31.          v_QuoteName = ifnull(r_Result.get("Quote_Name"),"-")
  32.          v_QuoteStage = ifnull(r_Result.get("Quote_Stage"),"-")
  33.          v_QuoteCurr = ifnull(r_Result.get("Quote_Currency"),"GBP")
  34.          v_QuoteTotal = ifnull(r_Result.get("Total_Amount"),0.0).toDecimal()
  35.          v_ExchangeRate = ifnull(r_Result.get("Exchange_Rate"),1)
  36.          v_ModifiedTime = ifnull(r_Result.get("Modified_Time"),r_Result.get("Added_Time")).toTime().toString("dd/MM/yyyy hh:mm a")
  37.          // 
  38.          // future proofs 
  39.          v_ExchangeRate = if(v_ExchangeRate == 0,1,v_ExchangeRate)
  40.          // 
  41.          // transforms 
  42.          if(r_Result.get("Valid_Till") != null) 
  43.          { 
  44.              v_DateTill = r_Result.get("Valid_Until").toDate().toString("dd/MM/yyyy")
  45.          } 
  46.          if(v_QuoteTotal != 0) 
  47.          { 
  48.              v_QuoteTotalGBP = (v_QuoteTotal.toDecimal() / v_ExchangeRate).toDecimal().round(2)
  49.              // 
  50.              // format currency with commas (thousand separator) and 2 decimals 
  51.              v_QuoteTotalStr = v_QuoteTotal.toDecimal().round(2).toString().replaceAll("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)",",")
  52.              v_QuoteTotalGBPStr = v_QuoteTotalGBP.toDecimal().round(2).toString().replaceAll("(?<!\.\d)(?<=\d)(?=(?:\d\d\d)+\b)",",")
  53.              // 
  54.              // to display both side by side if different otherwise display GBP 
  55.              if(!isnull(m_Currencies.get(v_QuoteCurr))) 
  56.              { 
  57.                  if(v_QuoteCurr == "GBP") 
  58.                  { 
  59.                      v_QuoteTotalDisp = m_Currencies.get(v_QuoteCurr) + " " + v_QuoteTotalGBPStr; 
  60.                  } 
  61.                  else 
  62.                  { 
  63.                      v_QuoteTotalDisp = m_Currencies.get(v_QuoteCurr) + " " + v_QuoteTotalStr + (£ " + v_QuoteTotalGBPStr + ")"
  64.                  } 
  65.              } 
  66.              else 
  67.              { 
  68.                  v_QuoteTotalDisp = v_QuoteTotalStr + " " + v_QuoteCurr + (£ " + v_QuoteTotalGBPStr + ")"
  69.              } 
  70.          } 
  71.          v_RelatedListXML = v_RelatedListXML + "<FL val=\"Quote Ref\">" + v_QuoteRef + "</FL>"
  72.          // don't forget to escape quote name with CDATA in case of XML banned characters 
  73.          v_RelatedListXML = v_RelatedListXML + "<FL val=\"Quote Name\" link=\"true\" url=\"" + v_LinkUrl2 + "\"><![CDATA[" + v_QuoteName + "]]></FL>"
  74.          v_RelatedListXML = v_RelatedListXML + "<FL val=\"Valid Till\">" + v_DateTill + "</FL>"
  75.          v_RelatedListXML = v_RelatedListXML + "<FL val=\"Stage\">" + v_QuoteStage + "</FL>"
  76.          v_RelatedListXML = v_RelatedListXML + "<FL val=\"Total Amount\">" + v_QuoteTotalDisp + "</FL>"
  77.          v_RelatedListXML = v_RelatedListXML + "<FL val=\"Modified Time\">" + v_ModifiedTime + "</FL>"
  78.          v_RelatedListXML = v_RelatedListXML + "</row>"
  79.          v_Index = v_Index + 1
  80.      } 
  81.      v_RelatedListXML = v_RelatedListXML + "</record>"
  82.  } 
  83.  else 
  84.  { 
  85.      // display "no records found" 
  86.      v_RelatedListXML = "<error><message>"
  87.      v_RelatedListXML = v_RelatedListXML + "No records found"
  88.      v_RelatedListXML = v_RelatedListXML + "</message></error>"
  89.  } 
  90.  // 
  91.  // output 
  92.  return v_RelatedListXML; 

in Zoho Books
copyraw
//
// find all related quotes
l_DataRows = List();
v_QuoteRefID = "0";
v_CrmOppID = "0";
v_OwnerName = <AppOwner>;
v_AppLinkName = <AppName>;
v_FormLinkName = <FormName>;
v_ViewLinkName = <ReportName>;
if(!isnull(salesorder.get("salesorder_id")))
{
	v_CreatorSoID = salesorder.get("salesorder_id");
}
v_SearchCriteria = "Sales_Order_ID==\"" + v_CreatorSoID + "\"";
r_SearchResults = zoho.creator.getRecords(v_OwnerName,v_AppLinkName,v_ViewLinkName,v_SearchCriteria,1,100,"joels_connnection");
v_ResponseCode = ifnull(r_SearchResults.get("code"),500).toLong();
if(v_ResponseCode == 3000)
{
	l_HeaderColumns = List();
	l_HeaderColumns.add({"key":"quote_name","value":"Name"});
	l_HeaderColumns.add({"key":"valid_till","value":"Valid Till"});
	l_HeaderColumns.add({"key":"quote_status","value":"Quote Status"});
	l_SearchResults = r_SearchResults.get("data").toJSONList();
	for each  r_Result in l_SearchResults
	{
		v_CreatorQuoteID = r_Result.get("ID");
		m_DataRow = Map();
		m_LinkedValue = Map();
		m_LinkedValue.put("value",r_Result.get("Quote_Name"));
		m_LinkedValue.put("isExternal",true);
		m_LinkedValue.put("link","https://app.zohocreator.eu/"+v_OwnerName+"/"+v_AppLinkName+"/"+v_FormLinkName+"/record-edit/"+v_ViewLinkName+"/"+v_CreatorQuoteID+"?param1=my_param_value");
		v_DateTill = "-";
		if(!isnull(r_Result.get("Valid_Till")))
		{
			v_DateTill = r_Result.get("Valid_Till").toDate().toString("dd/MM/yyyy");
		}
		m_DataRow.put("quote_name",m_LinkedValue);
		m_DataRow.put("valid_till",v_DateTill);
		m_DataRow.put("quote_status",r_Result.get("Quote_Status"));
		l_DataRows.add(m_DataRow);
	}
}
else
{
	l_HeaderColumns = List();
	l_HeaderColumns.add({"key":"message","value":"Message"});
	//
	if(v_ResponseCode == 3100)
	{
		m_DataRow = Map();
		m_DataRow.put("message","No records found");
		l_DataRows.add(m_DataRow);
	}
	else
	{
		m_DataRow = Map();
		m_DataRow.put("message",r_SearchResults.get("message") + " :: " + v_ResponseCode);
		l_DataRows.add(m_DataRow);
	}
}
//
m_RelatedList = Map();
m_RelatedList.put("header_context",l_HeaderColumns);
m_RelatedList.put("data",l_DataRows);
return m_RelatedList;
  1.  // 
  2.  // find all related quotes 
  3.  l_DataRows = List()
  4.  v_QuoteRefID = "0"
  5.  v_CrmOppID = "0"
  6.  v_OwnerName = <AppOwner>
  7.  v_AppLinkName = <AppName>
  8.  v_FormLinkName = <FormName>
  9.  v_ViewLinkName = <ReportName>
  10.  if(!isnull(salesorder.get("salesorder_id"))) 
  11.  { 
  12.      v_CreatorSoID = salesorder.get("salesorder_id")
  13.  } 
  14.  v_SearchCriteria = "Sales_Order_ID==\"" + v_CreatorSoID + "\""
  15.  r_SearchResults = zoho.creator.getRecords(v_OwnerName,v_AppLinkName,v_ViewLinkName,v_SearchCriteria,1,100,"joels_connnection")
  16.  v_ResponseCode = ifnull(r_SearchResults.get("code"),500).toLong()
  17.  if(v_ResponseCode == 3000) 
  18.  { 
  19.      l_HeaderColumns = List()
  20.      l_HeaderColumns.add({"key":"quote_name","value":"Name"})
  21.      l_HeaderColumns.add({"key":"valid_till","value":"Valid Till"})
  22.      l_HeaderColumns.add({"key":"quote_status","value":"Quote Status"})
  23.      l_SearchResults = r_SearchResults.get("data").toJSONList()
  24.      for each  r_Result in l_SearchResults 
  25.      { 
  26.          v_CreatorQuoteID = r_Result.get("ID")
  27.          m_DataRow = Map()
  28.          m_LinkedValue = Map()
  29.          m_LinkedValue.put("value",r_Result.get("Quote_Name"))
  30.          m_LinkedValue.put("isExternal",true)
  31.          m_LinkedValue.put("link","https://app.zohocreator.eu/"+v_OwnerName+"/"+v_AppLinkName+"/"+v_FormLinkName+"/record-edit/"+v_ViewLinkName+"/"+v_CreatorQuoteID+"?param1=my_param_value")
  32.          v_DateTill = "-"
  33.          if(!isnull(r_Result.get("Valid_Till"))) 
  34.          { 
  35.              v_DateTill = r_Result.get("Valid_Till").toDate().toString("dd/MM/yyyy")
  36.          } 
  37.          m_DataRow.put("quote_name",m_LinkedValue)
  38.          m_DataRow.put("valid_till",v_DateTill)
  39.          m_DataRow.put("quote_status",r_Result.get("Quote_Status"))
  40.          l_DataRows.add(m_DataRow)
  41.      } 
  42.  } 
  43.  else 
  44.  { 
  45.      l_HeaderColumns = List()
  46.      l_HeaderColumns.add({"key":"message","value":"Message"})
  47.      // 
  48.      if(v_ResponseCode == 3100) 
  49.      { 
  50.          m_DataRow = Map()
  51.          m_DataRow.put("message","No records found")
  52.          l_DataRows.add(m_DataRow)
  53.      } 
  54.      else 
  55.      { 
  56.          m_DataRow = Map()
  57.          m_DataRow.put("message",r_SearchResults.get("message") + :: " + v_ResponseCode)
  58.          l_DataRows.add(m_DataRow)
  59.      } 
  60.  } 
  61.  // 
  62.  m_RelatedList = Map()
  63.  m_RelatedList.put("header_context",l_HeaderColumns)
  64.  m_RelatedList.put("data",l_DataRows)
  65.  return m_RelatedList; 

Error(s) Encountered

Category: Zoho :: Article: 756