Welcome back, everyone!
Business scenario:
The Deals module in Zoho CRM helps keep track of all your business opportunities. To ensure proper lead qualification process with all specifics, it is recommended to first create a lead, convert it into a contact, and simultaneously create and associate a deal with the contact. This when done with field mapping, helps retain all mapped details from the lead record to contact record and subsequently to the deal record.
However, not all businesses require creating a deal while converting the lead to a contact. Moreover, deal creation might come at a much later stage in some organizations. There is an inconvenience associated with this approach. While the deal gets associated with the contact record, all other details updated in the leads/contacts record doesn’t get updated in the deal record. Updating them manually defeats the purpose. In comes this week's custom function to the rescue. Add a button linked to this custom function in your Contacts module and update relevant details from the contact records inside the deal records in a jiffy. Use this custom function to automatically update the fields listed below from contact records to deal records:
- Contact Name
- Account Name
- Title
- Email
- Phone
- Mobile
- Lead Source
- Email Opt Out
- Website
- Address Information
- Description
- Related activities
Before proceeding, ensure that you create the relevant custom fields in Deals module.
The scope of this custom function can be enhanced even further. Depending on your line of business, you might have certain permutations and combinations possible in your customer deals. Take, for example, the case of an automobile dealer. Say there are 3 variants of a particular car model with 4 different colors and various accessories options. Instead of updating these details manually, write a custom function for each of these combinations and associate that with a button. If a customer wants a specific combination that includes model, color, and enhancements, press the corresponding button and get the relevant fields updated. The code for this week doesn't include these scenarios. This is just to highlight the power of possibilities with custom functions.
Having to create multiple custom buttons might be a little time-consuming, but it's a one-time process and the benefits are worth the effort.
Getting started with the custom function:
- Go to Setup > Customization > Modules and Fields > Select the 'Contacts' module > Links and Buttons > +Create new button.
- Provide a name for the button. For example: “[The deal name]”. Add a description(optional).
- Choose View page from the drop-down list.
- Select Writing custom function from the subsequent drop-down.
- Provide a name for the custom function. Add a description(optional).
- Click “Free flow scripting”.
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “contId” and select the value as “Contact Id”.
- Save the changes.
Note:
- In Zoho CRM, the Deals module was formerly referred to as Potentials.
The script:
Code for Version 2.0 API:
contDetails = zoho.crm.getRecordById("Contacts", input.contId.toLong());
mp=map();
mp.put("Deal_Name",ifnull(contDetails.get("Full_Name"),""));
mp.put("Owner",ifnull(contDetails.get("Owner"),"").get("id"));
mp.put("Contact_Name",input.contId);
mp.put("Account_Name",ifnull(contDetails.get("Account_Name"),"").get("id"));
mp.put("Title",ifnull(contDetails.get("Title"),""));
mp.put("Email",ifnull(contDetails.get("Email"),""));
mp.put("Phone",ifnull(contDetails.get("Phone"),""));
mp.put("Mobile",ifnull(contDetails.get("Mobile"),""));
mp.put("Lead_Source",ifnull(contDetails.get("Lead_Source"),""));
mp.put("Email_Opt_Out",ifnull(contDetails.get("Email_Opt_Out"),""));
mp.put("Website",ifnull(contDetails.get("Website"),""));
mp.put("Mailing_Street",ifnull(contDetails.get("Mailing_Street"),""));
mp.put("Mailing_City",ifnull(contDetails.get("Mailing_City"),""));
mp.put("Mailing_State",ifnull(contDetails.get("Mailing_State"),""));
mp.put("Mailing_Zip",ifnull(contDetails.get("Mailing_Zip"),""));
mp.put("Mailing_Country",ifnull(contDetails.get("Mailing_Country"),""));
mp.put("Description",ifnull(contDetails.get("Description"),""));
create = zoho.crm.create("Deals", mp);
info create;
TaskDetails = zoho.crm.getRelatedRecords("Tasks","Contacts", input.contId.toLong());
for each task in TaskDetails
{
taskmap = Map();
taskmap.put("What_Id",create.get("id"));
taskmap.put("$se_module","Deals");
updatetask = zoho.crm.update("Tasks",task.get("id"),taskmap);
info updatetask;
}
EventDetails = zoho.crm.getRelatedRecords("Events","Contacts", input.contId.toLong());
for each events in EventDetails
{
eventmap = Map();
eventmap.put("What_Id",create.get("id"));
eventmap.put("$se_module","Deals");
updateevent = zoho.crm.update("Events",events.get("id"),eventmap);
info updateevent;
}
CallDetails = zoho.crm.getRelatedRecords("Calls","Contacts", input.contId.toLong());
for each call in CallDetails
{
callmap = Map();
callmap.put("What_Id",create.get("id"));
callmap.put("$se_module","Deals");
updatecall = zoho.crm.update("Calls",call.get("id"),callmap);
info updatecall;
}
return "Success";
Code for Version 1.0 API:
contactIdStr = input.contId.toString();
contDetails = zoho.crm.getRecordById("Contacts", input.contId.toLong());
name = ifnull(contDetails.get("First Name"),"")+ " " + ifnull(contDetails.get("Last Name"),"");
mp=map();
mp.put("Potential Name",name);
mp.put("SMOWNERID",ifnull(contDetails.get("SMOWNERID"),""));
mp.put("CONTACTID",input,contId);
mp.put("Account Name",ifnull(contDetails.get("Account Name"),""));
mp.put("Title",ifnull(contDetails.get("Title"),""));
mp.put("Email",ifnull(contDetails.get("Email"),""));
mp.put("Phone",ifnull(contDetails.get("Phone"),""));
mp.put("Mobile",ifnull(contDetails.get("Mobile"),""));
mp.put("Lead Source",ifnull(contDetails.get("Lead Source"),""));
mp.put("Email Opt Out",ifnull(contDetails.get("Email Opt Out"),""));
mp.put("Website",ifnull(contDetails.get("Website"),""));
mp.put("Mailing Street",ifnull(contDetails.get("Mailing Street"),""));
mp.put("Mailing City",ifnull(contDetails.get("Mailing City"),""));
mp.put("Mailing State",ifnull(contDetails.get("Mailing State"),""));
mp.put("Mailing Zip",ifnull(contDetails.get("Mailing Zip"),""));
mp.put("Mailing Country",ifnull(contDetails.get("Mailing Country"),""));
mp.put("Description",ifnull(contDetails.get("Description"),""));
create = zoho.crm.create("Potentials", mp);
info create;
newpotId = create.get("Id");
TaskDetails = zoho.crm.getRelatedRecords("Tasks","Contacts", contactIdStr);
for each ele in TaskDetails
{
TaskId=ele.get("ACTIVITYID");
taskMap=map();
taskMap.put("SEID",newpotId);
taskMap.put("SEMODULE","Potentials");
UpdateTask = zoho.crm.updateRecord("Tasks",TaskId,taskMap);
info UpdateTask;
info taskMap;
}
EventDetails = zoho.crm.getRelatedRecords("Events","Contacts", contactIdStr);
for each ele in EventDetails
{
eventId=ele.get("ACTIVITYID");
eventMap=map();
eventMap.put("SEID",newpotId);
eventMap.put("SEMODULE","Potentials");
UpdateEvent = zoho.crm.updateRecord("Events",eventId,eventMap);
info UpdateEvent;
info eventMap;
}
CallDetails = zoho.crm.getRelatedRecords("Calls","Contacts", contactIdStr);
for each ele in CallDetails
{
callId=ele.get("ACTIVITYID");
callMap=map();
callMap.put("SEID",newpotId);
callMap.put("SEMODULE","Potentials");
UpdateCall = zoho.crm.updateRecord("Calls",callId,callMap);
info UpdateCall;
info callMap;
}
return "Success";
Note:
- Create multiple buttons to contain multiple scenarios of deals, and save the time to create a deal with the particular information.
- This custom function works only when you create a deal using the linked button.
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.
I'm sure you would have planned your year-end celebrations already. We'd be back in Jan '18 with the next custom function. Merry Christmas and a Happy New Year. See you!
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.