Welcome back folks!
Last week, we learnt about auto-update of account information across CRM. This week, let's look at a custom function that makes accounting and report generation easier by automatically calculating the amount of tax, discounts and the quantity of items for a particular record in a quote, invoice, purchase order or sales order.
Business scenario:
I'm sure you agree that meticulous tracking and management is critical for your business to hold up and flourish. You need to keep an inventory of items and products you have or deal with. The other important aspects of the transactions that need keeping a tab on, are taxes and discounts associated with records in a quote, purchase order or sales order. Do you want to manually do that on your own? Of course not. This custom function helps automate just that. It sums up the total amount of tax to be paid and the discount given to the customer, and lists the total quantity of products involved in the deal.
The quotes, sales order, purchase order and invoices of a deal are tracked in your CRM solution. Add this custom function to any or all of the four modules. See the total taxes, discounts, and quantity of products against the records.
Let's assume an example of calculating the taxes and discounts for a record in the Quotes module.
The above record displays the products involved in the deal in the "Product details" section and the "Line Item totals" section is where the total tax, discounts, and total quantity of products are displayed.
Pre-requisite: Create the custom fields, "Total Tax at line item level", "Total discount at line item level" and "Total quantity".
Getting started with the custom function:
- Go to Setup>Automations>Actions >Custom Functions > Configure Custom Function > Write your own.
- Provide a name for the Custom function. For example: “Sumup line items field values”.
- Select the module as Quotes. Add a description(optional).
- Click “Free flow scripting”.
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “quoteId” and select the value as “Quote Id”.
- Save the changes.
The script:
Code for Version 2.0 API:
1) For total tax:
respMap = zoho.crm.getRecordById("Quotes", input.quoteId.toLong());
productDet = ifnull(respMap.get("Product_Details"),"");
linetaxes = 0.0;
for each eachProd in productDet
{
linetax = (ifnull(eachProd.get("Tax"),"0.0")).toDecimal();
linetaxes = (linetaxes + linetax);
}
paramap = map();
paramap.put("Total_Tax_at_line_item_level", linetaxes);
updateResp = zoho.crm.updateRecord("Quotes", quoteId.toLong(), paramap);
info paramap;
info updateResp;
2) For total discount :
quoteIdStr = input.quoteId.toString();
respMap = zoho.crm.getRecordById("Quotes", input.quoteId.toLong());
productDet = ifnull(respMap.get("Product_Details"),"");
linediscount = 0.0;
for each eachProd in productDet
{
linedis = (ifnull(eachProd.get("Discount"),"0.0")).toDecimal();
info linedis;
linediscount = (linediscount + linedis);
}
paramap = map();
paramap.put("Total_Discount_at_line_item_level", linediscount);
updateResp = zoho.crm.update("Quotes", quoteId.toLong(), paramap);
info paramap;
info updateResp;
3) For total quantity :
quoteIdStr = input.quoteId.toString();
quoteMap = zoho.crm.getRecordById("Quotes", input.quoteId.toLong());
productDet = ifnull(quoteMap.get("Product_Details"),"");
value = 0;
for each eachProd in productDet
{
qty = (ifnull(eachProd.get("quantity"),"0")).toLong();
value = value + qty ;
}
params = map();
params.put("Total_Quantity", value);
updateResp = zoho.crm.update("Quotes", quoteId.toLong(), params);
info params;
info updateResp;
Code for Version 1.0 API:
1) For total tax:
quoteIdStr = input.quoteId.toString();
respMap = zoho.crm.getRecordById("Quotes", input.quoteId);
productDet = ifnull(respMap.get("product"),"");
productList = productDet.toJSONList();
linetaxes = 0.0;
for each eachProd in productList
{
eachProdDet = eachProd.toMap();
linetax = (ifnull(eachProdDet.get("Tax"),"0.0")).toDecimal();
linetaxes = (linetaxes + linetax);
}
paramap = map();
paramap.put("Total Tax at line item level", linetaxes);
updateResp = zoho.crm.updateRecord("Quotes", quoteIdStr, paramap);
info paramap;
info updateResp;
2) For total discount :
quoteIdStr = input.quoteId.toString();
respMap = zoho.crm.getRecordById("Quotes", input.quoteId);
productDet = ifnull(respMap.get("product"),"");
productList = productDet.toJSONList();
linediscount = 0.0;
for each eachProd in productList
{
eachProdDet = eachProd.toMap();
linedis = (ifnull(eachProdDet.get("Discount"),"0.0")).toDecimal();
info linedis;
linediscount = (linediscount + linedis);
}
paramap = map();
paramap.put("Total Discount at line-item level", linediscount);
updateResp = zoho.crm.updateRecord("Quotes", quoteIdStr, paramap);
info paramap;
info updateResp;
3) For total quantity :
quoteIdStr = input.quoteId.toString();
quoteMap = zoho.crm.getRecordById("Quotes", input.quoteId);
productDet = ifnull(quoteMap.get("product"),"");
productList = productDet.toJSONList();
value = 0;
for each eachProd in productList
{
eachProdDet = eachProd.toMap();
qty = (ifnull(eachProdDet.get("Quantity"),"0")).toLong();
value = value + qty ;
}
params = map();
params.put("Total Quantity", value);
updateResp = zoho.crm.updateRecord("Quotes", quoteIdStr, params);
info params;
info updateResp;
Note:
- To add this custom function to the other modules such as SalesOrders, Invoices, or PurchaseOrders, make sure you replace the module name Quote to the desired module name in the snippet above.
Found this useful? Try and let me know how it works! If you have questions, do not hesitate to ask! Share this with your team if you find it useful. See you all next week with another interesting custom function. Ciao!
Update: As you must be aware, API V1.0 will be deprecated and support for version 1.0 API will be available only till Dec 31, 2018. Version 1.0 compatible Functions will continue to work until Dec 31, 2019. You're advised to migrated to API Version 2.0 at the earliest. Check this announcement for more. We've updated the post to include the Version 2.0 compatible Function.