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:


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;

 

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;

 

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;

 

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