Get the total quantity, taxes and discounts for a record at a glance!

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" (Currency Field), "Total discount at line item level" (Currency Field) and "Total quantity" (Number Field).

Getting started with the function:

  • Go to Setup > Customization > Modules > Deals > Links and buttons > Create new button.
  • Provide a name for the button. For example: "Sum line items field values". Add a description(optional).
  • Select the module to be associated as Quotes.
  • Select the action to be performed as "Writing function".
  • Copy the code given below.
  • Click "Edit arguments".
  • Enter the name as "quoteId" and select the value as "Quote Id".
  • Click Save & Execute Script.
  • Save the changes.

The Code:

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;

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;

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.
  • The code is zoho.crm._getRecordById for Version 1.0 of APIs.

Found this useful? Try it out 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!

Return to Tips