Welcome back people!
Last week, we looked at a custom function that enabled you to create purchase orders from sales orders at just the click of a button. This week, let's look at another custom function which lets you populate the Amount field in a Deal by calculating the number of products and its unit price.
Business Scenario:
I’m sure you’d agree that the Deal Dashboards is one of the most sought after widgets in Zoho CRM. One of my favorite ones is “Pipeline by Stage” widget that gives a clear indication of your sales pipeline across various deal stages. It gives a fair idea of your revenue pipeline, helps you understand whether you’d be able to meet your sales targets, and take necessary corrective actions if required.
Here comes the tricky part. To make better use of these dashboards, your users have to update all the deal values diligently. Unless the Amount field in the deal is filled accurately, the pipeline dashboard is of little use. Filling the Amount field manually creates a room for error. Moreover, the field could be left blank initially and be updated only at later stages of the deal. Automation removes the scope for such errors. This week’s custom function helps address this problem with ease. The Amount field is dependent on the product details included in the deal. You don't have to calculate the amount that is to be transacted in the deal. This custom function populates the "Amount" field with the information from the "Products" Related List.
Amount field = (Unit price of P1 + Unit price of P2+....).
P1, P2, .... denote the individual products in the deal.
Getting started with the custom function:
- Go to Setup > Automation > Actions > Custom Functions > Configure Custom Function > Write your own.
- Provide a name for the button. For example: “Auto-populate deal amount”. Add a description(optional).
- Choose the module as "Deals".
- Click “Free flow scripting”.
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “potId” and select the value as “Deal Id”.
- Save the changes.
The script:
Code for Version 2.0 API:
RelatedPotential = zoho.crm.getRelatedRecords("Products", "Deals", potId.toLong());
//info RelatedPotential;
unitprice = 0.0 ;
for each ele in RelatedPotential
{
unit = ifnull(ele.get("Unit_Price"),"0.0").toDecimal();
unitprice = unitprice + unit ;
}
mp=map();
mp.put("Amount",unitprice);
update=zoho.crm.update("Deals", potId.toLong(), mp);
info mp;
info update;
Code for Version 1.0 API:
potIdStr = input.potId.toString();
RelatedPotential = zoho.crm.getRelatedRecords("Products", "Potentials", potIdStr,1,200);
//info RelatedPotential;
unitprice = 0.0 ;
for each ele in RelatedPotential
{
unit = ifnull(ele.get("Unit Price"),"0.0").toDecimal();
unitprice = unitprice + unit ;
}
mp=map();
mp.put("Amount",unitprice);
update=zoho.crm.updateRecord("Potentials", potIdStr, mp);
info mp;
info update;
Note:
- In Zoho CRM, the "Deals" module was formerly known as "Potentials" module.
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.
Do check out other custom functions shared in this series.
Ciao! Stay tuned folks!
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.
Edit (5-May-2020):
Disclaimer: The code works when the quantity of products involved in the deal is 1. It sums up the unit price of the products added to the deal and updates in the Amount field. Sincere thanks to "Brian N." for pointing this out! For scenarios with product quantity greater than one, you might want to consider using the Quotes involved in the deal. Check this post for reference. Two scenarios are covered in that post. You might want to consider the value from the latest quote as requested by Bram Lyng Andersen or use the sum of all quote values associated with the respective deal. Use as you see fit and feel free to ask if you have any questions.