Welcome back everyone!
Business scenario:
You hit a stage in a deal when you know it would follow through. This is when you must create a Quote. which is a legal agreement between a prospect and a vendor. Wouldn't it be cool to add a button to the deals module to automatically create a Quote from within the deal, on just a click of a button? Create a Quote at any stage of the deal and associate with the Quote, and save time!
Besides, the quote has the product details specific to the deal, leading to a simultaneously updated inventory as well. Two birds with one stone and all that :)
Getting started with the custom function:
- Go to Setup > Customization > Modules > Deals > Links and buttons >Create new button .
- Provide a name for the button. For example: “Create Quote from deal”. Add a description(optional).
- Select the placement of the button as View page .
- Select the action to be performed as " Writing custom function ".
- Click “ Free flow scripting ”.
- Copy the code given below.
- Click “ Edit arguments ”.
- Enter the name as “ potIdStr ” and select the value as “ Potential Id ”.
- Save the changes.
The script:
Code for Version 2.0 API:
- potDetails = zoho.crm.getRecordById("Deals",potIdStr.toLong());
- paramap = Map();
- if( ifnull(potDetails.get("Contact_Name"),"") != "")
- {
- contact = ifnull(potDetails.get("Contact_Name").get("id"),"");
- paramap.put("Contact_Name",contact);
- }
- if( ifnull(potDetails.get("Account_Name"),"") != "")
- {
- account = ifnull(potDetails.get("Account_Name").get("id"),"");
- paramap.put("Account_Name",account);
- }
- RelatedProducts = zoho.crm.getRelatedRecords("Products","Deals",potIdStr.toLong());
- product_items = List();
- for each product in RelatedProducts
- {
- proid = product.get("id");
- proname = product.get("Product_Name");
- productDesc= ifnull(product.get("Description"),"Manque Description Produit !!!");
- quantity = 1;
- price = ifnull(product.get("Unit_Price"),"0.0").toDecimal();
- listprice = price * quantity;
- lineitem = Map();
- lineitem.put("product",{"name":proname,"id":proid});
- lineitem.put("product_description",productDesc);
- lineitem.put("quantity",quantity);
- lineitem.put("net_total",listprice);
- lineitem.put("total",listprice);
- lineitem.put("list_price",listprice);
- product_items.add(lineitem);
- }
- paramap.put("Product_Details",product_items);
- paramap.put("Subject",ifnull(potDetails.get("Deal_Name"),""));
- paramap.put("Deal_Name",potIdStr.toLong());
- paramap.put("Description",ifnull(potDetails.get("Description"),""));
- createResp = zoho.crm.createRecord("Quotes", paramap);
- info paramap;
- info createResp;
- newid = ifnull(createResp.get("id"),"");
- if ( newid != "")
- {
- openUrl( "https://crm.zoho.com/crm/EntityInfo.do?module=Quotes&id=" + newid, "same window");
- return "New Quote created successfuly";
- }
- else
- {
- return "Quote could not be created. Missing product details in Deal.";
- }
Code for Version 1.0 API:
potIdStr = input.potId.toString();
potDetails = zoho.crm.getRecordById("Potentials", input.potId.toLong());
RelatedProducts = zoho.crm.getRelatedRecords("Products", "Potentials", potIdStr);
quotesubject = ifnull(potDetails.get("Potential Name"),"");
contact = ifnull(potDetails.get("CONTACTID"),"");
account = ifnull(potDetails.get("ACCOUNTID"),"");
description = ifnull(potDetails.get("Description"),"");
pdlist = List();
sub = 0.0;
for each eachProd in RelatedProducts
{
productDesc = ifnull(eachProd.get("Product Description"),"");
quantity = 1;
productId = ifnull(eachProd.get("PRODUCTID"),"");
price = (ifnull(eachProd.get("Unit Price"),"0.0")).toDecimal();
listprice = (price * quantity);
mp = map();
mp.put("Product Id", productId);
mp.put("Quantity", quantity);
mp.put("List Price", listprice);
mp.put("Net Total", listprice);
pdlist.add(mp);
sub = (sub + listprice);
}
paramap = map();
paramap.put("Products", pdlist);
paramap.put("Subject", quotesubject);
paramap.put("CONTACTID", contact);
paramap.put("ACCOUNTID", account);
paramap.put("POTENTIALID", input. potId);
paramap.put("Description", description);
paramap.put("Sub Total", sub);
paramap.put("Grand Total", sub);
createResp = zoho.crm.create("Quotes", paramap);
info createResp;
newid = createResp.get("Id");
return "Quote Created";
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. 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.
Edited on 18-May-2020:
- As Contact and Account names are not mandatory, the code has been updated with a null check to skip them when they are empty, and update them in the Quote when available.
- Product field cannot be empty while creating quotes. This code fetches the product details from the Deal and if the code is executed on a Deal record with empty product field, it will prompt the user to check the same.
- As of now, the product quantity is taken as one in product field of Deals. This is a system limitation, and if your sale involves a quantity greater than one, you will have to update it manually in the newly created quote. The Quote opens in a new tab immediately upon creation.