Function-4: Get the total quantity, taxes and discounts for a record at a glance!

Function-4: Get the total quantity, taxes and discounts for a record at a glance!




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.
    Zoho Marketing Automation
            • Sticky Posts

            • Focus Group : Functions 101 - A webinar series

              A lot of businesses require their CRM to perform activities that are out-of-the-box. And dynamic as Zoho CRM is, a lot of these scenarios can be achieved by running simple functions. They help you update data in CRM modules or in third-party applications and automate most of your business processes.  We're organizing a four part webinar series to help you build your own functions from scratch. This series breaks down the myth that functions are complex. Follow this link to register.    Note - If
            • Function-2: Round-Robin assignment of records

              Welcome back folks! Last week, we saw how to update sales commission in quotes using a custom function. This week, let's see an interesting use case asked by many of you - auto-assignment records by round-robin method. Business scenario: Right now, the solution allows you to auto-assign leads from web form and imported lists. Let us look at a need where you want to auto-assign leads from in-bound calls in a round-robin method, across modules. Prerequisite: You must create a permanent record in the
            • Function #50: Schedule Calls to records

              Welcome back everyone! Last week's function was about changing ownership of multiple records concurrently. This week, it's going to be about scheduling calls for records in various modules. Business scenario Calls are an integral part of most sales routines.. Sales, Management, Support, all the branches of the business structure would work in cohesion only through calls. You could say they are akin to engine oil, which is required by the engine to make all of it's components function perfectly. CRM
            • Custom Function: Tracking the Case Closed time

              Scenario: Tracking the Case Closed time   We create cases and close them but how do we keep track of the details on when the case was closed? This custom function helps you track the closed time of a case, in a custom date-time field. You can then generate reports based on the closed time. You need to have a custom date time field to update the Closed Time. Please follow these steps: Log in to Zoho CRM with administrative privileges. Click Setup > Automation > Workflow > Create Rule. In the New Rule
            • Function #37: Create a Purchase Order from a Quote

              Welcome back everyone! Last week, we learnt how to calculate the total number of activities for a lead and further take note of the activity count for particular dates. For instance, from the period of Demo to Negotiation. This week, let's look at a function that lets you create a Purchase Order instantly from a Quote. Business scenario: In any form of business, one of the most important things to do is to document the transactions. Naturally, negotiation, signing an agreement, placing an order,

            Zoho Pagesense Resources

              Zoho SalesIQ Resources



                    Zoho TeamInbox Resources

                      Zoho DataPrep Resources



                        Zoho CRM Plus Resources

                          Zoho Books Resources


                            Zoho Subscriptions Resources

                              Zoho Desk Resources

                                Zoho Projects Resources


                                  Zoho Sprints Resources


                                    Qntrl Resources


                                      Zoho Creator Resources


                                        Zoho WorkDrive Resources




                                            Zoho Campaigns Resources

                                              Zoho CRM Resources

                                                      Design. Discuss. Deliver.

                                                      Create visually engaging stories with Zoho Show.

                                                      Get Started Now