Welcome back everyone!
Business scenario:
A typical business process flows like this: Leads -> Contacts -> Accounts -> Deals -> Contacts. If you're wondering why 'Contacts' is mentioned twice, it is to register the deal information and associate it with the Contact. Further, this paves the road for future deals. Similarly, a deal involves procurement or giving out services or products. As such, until the deal is completed both on paper and in actuality, there is a need to keep an inventory of the number of Products involved in each deal. This is done in the "Products" module.
You can have a subform (a form within a form) in the Contact record, which contains details of Products and Services procured by the customer. And if the customer buys your product at two different timelines (say 1-2 years apart), that information also needs to be given importance and registered.
The function for this week takes care of replicating the Product details in the Contact record's subform. The Deals module has a Related list for Products, listing the details of the products involved in the deal. Once a deal is closed (won), you can set this function up to replicate the details of the product in the subform of the Contact's record (customer's record).
Getting started with the custom function:
- Go to Setup > Automation > Actions > Functions > Configure Function > Write your own.
- Provide a name for the function. For example: "Product quantity update in Deal". Add a description (optional).
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “dealId” and select the value as “Deal Id”.
- Enter the name as “contId” and select the value as “Contact Id”.
- Click Save&Execute Script to check the code.
- Save the function.
The Code:
------------------------------------------------------------------------------------------------------------------------------------
RelatedProducts = zoho.crm.getRelatedRecords("Products", "Deals",input.dealId.toLong());
Details = zoho.crm.getRecordById("Contacts", contId.toLong());
sub_forms = List();
for each product in RelatedProducts
{
subform = Map();
subform.put("Product_Lookup", ifnull(product.get("id"),""));
subform.put("Product_Code", ifnull(product.get("Product_Code"),""));
subform.put("Product_Name", ifnull(product.get("Product_Name"),""));
sub_forms.add(subform);
}
parammap = Map();
parammap.put("Product_Details",sub_forms);
update = zoho.crm.update("Contacts",contId.toLong(),parammap);
info parammap;
info update;
------------------------------------------------------------------------------------------------------------------------------------
Note:
- The code given above works only for V2 version of Zoho APIs. Please note that the code WILL NOT work for Version 1.0 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. Do check out other custom functions shared in this series
here
.
See you all next week with another interesting custom function. Ciao!