Skip to content

Instantly share code, notes, and snippets.

@carpiediem
Last active August 18, 2021 13:49
Show Gist options
  • Save carpiediem/aab33b6053c112bfa7855247ec54fc19 to your computer and use it in GitHub Desktop.
Save carpiediem/aab33b6053c112bfa7855247ec54fc19 to your computer and use it in GitHub Desktop.
This script allows Zoho CRM users to add a new related list related to the Products module. It will list every Quote, Sales Order, Purchase Order, or Invoice in Zoho CRM that includes the current product as a line item.
string Orders(int productId)
{
// For use in Zoho CRM custom related lists
// 1. Open a product info page and click the + next to RELATED LIST
// 2. Click Custom Function & Create New Custom Function
// 3. Name the function "product_orders" and define a single argument named "productId" as a string
// 4. Switch to Free-Flow Scripting and paste in this code
productIdStr=input.productId.toString();
xml="";
row=0;
// Add Quotes (need to fix for when we have more data; Zoho limits these requests to 200 records)
everyQuote = zoho.crm.getRecords("Quotes",0,200);
for each quote in everyQuote
{
// Get the List:Map() of products as a string, then remove the first and last character
productString=quote.get("product");
productString=productString.subString(1,(productString.length() - 1));
// Split the sting along the Map() boundaries
productList=productString.toList("},{");
productFound=false;
for each lineItemString in productList
{
if(!productFound)
{
// Turn the line item's string back into a Map() object
lineItemString="{" + lineItemString + "}";
lineItem=lineItemString.toMap();
// Check if the current line item matches the function argument, productId
liProductId=lineItem.get("Product Id");
if(liProductId == productIdStr)
{
// If found, append a new row of XML and skip the remaining line items
productFound=true;
xml=xml + "<row no=\"" + row + "\"><FL val=\"Subject\" link=\"true\" url=\"https://crm.zoho.com/crm/EntityInfo.do?module=Quotes&id=" + quote.get("QUOTEID") + "\">" + quote.get("Subject") + "</FL><FL val=\"Type\">Quote</FL><FL val=\"Partner\" link=\"true\" url=\"https://crm.zoho.com/crm/EntityInfo.do?module=Accounts&id=" + quote.get("ACCOUNTID") + "\">" + quote.get("Account Name") + "</FL><FL val=\"Date\">" + quote.get("Valid Till") + "</FL><FL val=\"Status\">" + quote.get("Quote Stage") + "</FL><FL val=\"Quantity\">" + lineItem.get("Quantity").toLong() + "</FL><FL val=\"Unit Price\">$" + lineItem.get("List Price") + "</FL></row>";
row=(row + 1);
}
}
}
}
// Add Sales Orders
everySalesOrder = zoho.crm.getRecords("Salesorders",0,200);
for each so in everySalesOrder
{
productString=so.get("product");
productString=productString.subString(1,(productString.length() - 1));
productList=productString.toList("},{");
productFound=false;
for each lineItemString in productList
{
if(!productFound)
{
lineItemString="{" + lineItemString + "}";
lineItem=lineItemString.toMap();
liProductId=lineItem.get("Product Id");
if(liProductId == productIdStr)
{
productFound=true;
xml=xml + "<row no=\"" + row + "\"><FL val=\"Subject\" link=\"true\" url=\"https://crm.zoho.com/crm/EntityInfo.do?module=SalesOrders&id=" + so.get("SALESORDERID") + "\">" + so.get("Subject") + "</FL><FL val=\"Type\">Sales Order</FL><FL val=\"Partner\" link=\"true\" url=\"https://crm.zoho.com/crm/EntityInfo.do?module=Accounts&id=" + so.get("ACCOUNTID") + "\">" + so.get("Account Name") + "</FL><FL val=\"Date\">" + so.get("Order Date") + "</FL><FL value=\"Status\">" + so.get("Status") + "</FL><FL val=\"Quantity\">" + lineItem.get("Quantity").toLong() + "</FL><FL val=\"Unit Price\">$" + lineItem.get("List Price") + "</FL></row>";
row=row+1;
}
}
}
}
// Add Purchase Orders
everyPurchaseOrder = zoho.crm.getRecords("Purchaseorders",0,200);
for each po in everyPurchaseOrder
{
productString=po.get("product");
productString=productString.subString(1,productString.length()-1);
productList=productString.toList("},{");
productFound=false;
for each lineItemString in productList
{
if(!productFound)
{
lineItemString="{" + lineItemString + "}";
lineItem=lineItemString.toMap();
liProductId=lineItem.get("Product Id");
if(liProductId == productIdStr)
{
productFound=true;
xml=xml + "<row no=\"" + row + "\"><FL val=\"Subject\" link=\"true\" url=\"https://crm.zoho.com/crm/EntityInfo.do?module=PurchaseOrders&id=" + po.get("PURCHASEORDERID") + "\">" + po.get("Subject") + "</FL><FL val=\"Type\">Purchase Order</FL><FL val=\"Partner\" link=\"true\" url=\"https://crm.zoho.com/crm/EntityInfo.do?module=Vendors&id=" + po.get("VENDORID") + "\">" + po.get("Vendor Name") + "</FL><FL val=\"Date\">" + po.get("PO Date") + "</FL><FL value=\"Status\">" + po.get("Status") + "</FL><FL val=\"Quantity\">" + lineItem.get("Quantity").toLong() + "</FL><FL val=\"Unit Price\">$" + lineItem.get("List Price") + "</FL></row>";
row=(row + 1);
}
}
}
}
// Add Invoices
everyInvoice = zoho.crm.getRecords("Invoices",0,200);
for each invoice in everyInvoice
{
productString=invoice.get("product");
productString=productString.subString(1,(productString.length() - 1));
productList=productString.toList("},{");
productFound=false;
for each lineItemString in productList
{
if(!productFound)
{
lineItemString="{" + lineItemString + "}";
lineItem=lineItemString.toMap();
liProductId=lineItem.get("Product Id");
if(liProductId == productIdStr)
{
productFound=true;
xml=xml + "<row no=\"" + row + "\"><FL val=\"Subject\" link=\"true\" url=\"https://crm.zoho.com/crm/EntityInfo.do?module=Invoices&id=" + invoice.get("INVOICEID") + "\">" + invoice.get("Subject") + "</FL><FL val=\"Type\">Invoice</FL><FL val=\"Partner\" link=\"true\" url=\"https://crm.zoho.com/crm/EntityInfo.do?module=Accounts&id=" + invoice.get("ACCOUNTID") + "\">" + invoice.get("Account Name") + "</FL><FL val=\"Date\">" + invoice.get("Invoice Date") + "</FL><FL value=\"Status\">" + invoice.get("Status") + "</FL><FL val=\"Quantity\">" + lineItem.get("Quantity").toLong() + "</FL><FL val=\"Unit Price\">$" + lineItem.get("List Price") + "</FL></row>";
row=(row + 1);
}
}
}
}
// Return XML string
if(xml == "")
{
return "No Orders found for this Product.";
}
else
{
return "<record>" + xml.replaceAll("&","&amp;",false) + "</record>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment