Welcome back everyone!
Last week, we learnt how to populate the Amount field in a Deal by calculating the number of products and its unit price. This week, let's look at a custom function that updates information between two modules via Lookup fields.
Business scenario:
Quotes are binding agreements between a customer and a vendor, to deliver the requested products to the customer within a specified time-frame at a predefined price. Your customers can place orders within a stipulated period (validity date) that has been mentioned in the quote, otherwise you may cancel or extend the time-frame by sending a new quote. In general, a Quote contains the Quote Number, Date, Line Items(Products) inclusive of quantity and prices based on your Price Books, Terms & Conditions and Description.
And incidently, the quote can be sent to a specific Contact or an Account. Depending on the scenario, the information to be on the quote may differ. For instance, you may want to add the Contact Phone number, Email address, Description or any information in a custom field to the quote. The Quotes module indeed has a lookup field for Contact, Deal and Account. Meaning, the information from a Contact, Deal or an Account can be copied automatically to a Quote through the lookup. So where does this custom function fit in?
It's for the information fields which are not available in a Quote. Information which is not normally available to be stored in the CRM can be stored using a custom field. Similarly, the modules Quotes, Invoice and modules like that have to be updated to store that additional information too. Are you going to juggle between modules to copy each additional information when a lookup is already available?
The custom function for today latches on to the Lookup field and whenever a Contact is selected as a lookup, the custom fields specified in the custom function are also transferred automatically. Furthermore, this custom function is not limited to the Quotes module. You can use this function to any module that has a Lookup field.
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: “Contact Lookup-1”. Add a description(optional).
- Select the module as Quote. 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”.
- Enter the name as "contId" and select the value as "Contact Id".
- Save the changes.
The script:
Code for Version 2.0 API:
contDetails = zoho.crm.getRecordById("Contacts", input.contId.toLong());
mp=map();
mp.put("First_Name",ifnull(contDetails.get("First_Name"),""));
mp.put("Last_Name",ifnull(contDetails.get("Last_Name"),""));
mp.put("Broker_First_Name",ifnull(contDetails.get("Broker_First_Name"),""));
mp.put("Broker_Last_Name",ifnull(contDetails.get("Broker_Last_Name"),""));
mp.put("Property_Street",ifnull(contDetails.get("Property_Street"),""));
mp.put("Property_City",ifnull(contDetails.get("Property_City"),""));
mp.put("Property_State",ifnull(contDetails.get("Property_State"),""));
mp.put("Property_Zip",ifnull(contDetails.get("Property_Zip"),""));
updateResp= zoho.crm.update("Quotes",input.quoteId.toLong(), mp);
info mp;
info updateResp;
Code for Version 1.0 API:
quoteIdStr = input.quoteId.toString();
contDetails = zoho.crm.getRecordById("Contacts", input.contId);
mp=map();
mp.put("First Name",ifnull(contDetails.get("First Name"),""));
mp.put("Last Name",ifnull(contDetails.get("Last Name"),""));
mp.put("Broker First Name",ifnull(contDetails.get("Broker First Name"),""));
mp.put("Broker Last Name",ifnull(contDetails.get("Broker Last Name"),""));
mp.put("Property Street",ifnull(contDetails.get("Property Street"),""));
mp.put("Property City",ifnull(contDetails.get("Property City"),""));
mp.put("Property State",ifnull(contDetails.get("Property State"),""));
mp.put("Property Zip",ifnull(contDetails.get("Property Zip"),""));
updateResp= zoho.crm.updateRecord("Quotes", quoteIdStr, mp);
info mp;
info updateResp;
Adding to a workflow:
- Go to Setup > Automation > Workflow Rules.
- Click '+ Create Rule'.
- Select the Module for which this custom function has to be added and give it a name and a description(optional).
- Select "Create or Edit" in the "When do you want to execute this rule?".
- Select the Condition as "All Records" and click Next.
- Choose "Custom Function" from Instant Actions.
- Select the option "Custom Function" (Created by users from your organization).
- Select the required custom function and click Publish.
- Provide a Description for the custom function and Publish it.
- Save the workflow.
Note:
- You can use the code on any module. Change the name of the module from 'Quotes' to whichever module you prefer and update the code accordingly.
- The Fields in the left section (right after mp.put) of the code are the destination fields and the ones in the right are the source fields. Update your code according to your requirement and your fields.
- If there is a need to copy information from a custom field, you need to create that custom field for the destination module too.
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!
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.