Welcome back everyone!
Business scenario:
Opportunities come in strange ways. One day there might be no good leads, but the next day you might bag a million dollar deal. Or there might be a lot of deals with smaller figures from different leads. Either way, there's a lotta work for you. And by that, I mean gathering lead details, following up with them, documenting your interactions with them and so on. If there are multiple leads, you can imagine what you're gonna go through.
At times, leads would just become contacts and there are also times where leads would be from other companies. Zoho CRM offers you the provisions to automatically create a contact from a lead, but not an account. Wouldn't it save a lot of your work and time if an Account (company) record is created with all the details, interactions and information from a lead record?
Although it is conventional process to get a lead, create a contact and then add an account for the said lead, there might arise a situation where contacts are a section that are not required. This week's function is for those scenarios.
It works in the same way as Lead to Contact conversion, but just that it's Lead to Account.
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 Account from Lead". Add a description(optional).
- Select the placement of the button as View page.
- Select the action to be performed as "Writing function".
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “leadId” and select the value as “Lead Id”.
- Click Save&Execute Script.
- Save the script.
- Select the profiles who can view this button.
- Click Save.
The script:
Code for Version 2.0 API:
leadDetails = zoho.crm.getRecordById("Leads", leadId.toLong());
accountmap = map();
accountmap.put(("Account_Name"), ifnull(leadDetails.get("Full_Name"),""));
accountmap.put("App_Out", ifnull(leadDetails.get("App Out"),""));
accountmap.put("Approved_Date", ifnull(leadDetails.get("Approved_Date"),""));
accountmap.put("Associated_MIDs", ifnull(leadDetails.get("Associated_MIDs"),""));
accountmap.put("Transaction_Fee", ifnull(leadDetails.get("Transaction_Fee"),""));
accountmap.put("Type_of_Ownership", ifnull(leadDetails.get("Type_of_Ownership"),""));
accountmap.put("Website", ifnull(leadDetails.get("Website"),""));
accountmap.put("Billing_Code", ifnull(leadDetails.get("Zip_Code"),""));
accountmap.put("Yelp", ifnull(leadDetails.get("Yelp"),""));
accountcreate = zoho.crm.create(("Accounts"), accountmap);
newacctId = (accountcreate).get("id");
RelNotes = zoho.crm.getRelatedRecords("Notes", "Leads", leadId.toLong());
for each ele in RelNotes
{
notemap = Map();
notemap.put("Parent_Id",newacctId);
notemap.put("Note_Content",note.get("Note_Content"));
notemap.put("se_module","Accounts");
notecreate = zoho.crm.create("Notes",notemap);
info notecreate;
}
TaskDetails = zoho.crm.getRelatedRecords("Tasks", "Leads", leadId.toLong());
for each ele1 in TaskDetails
{
taskMap = map();
taskMap.put("What_Id", newacctId);
taskMap.put("se_module","Accounts");
UpdateTask = zoho.crm.update("Tasks", ele1.get("id"), taskMap);
}
EventDetails = zoho.crm.getRelatedRecords("Events", "Leads", leadId.toLong());
for each ele2 in EventDetails
{
eventMap = map();
eventMap.put("What_Id", newacctId);
eventMap.put("se_module","Accounts");
UpdateEvent = zoho.crm.update("Events", ele2.get("id"), eventMap);
}
CallDetails = zoho.crm.getRelatedRecords("Calls", "Leads", leadId.toLong());
for each ele3 in CallDetails
{
callMap = map();
callMap.put("What_Id", newacctId);
callMap.put("se_module","Accounts");
UpdateCall = zoho.crm.update("Calls", ele3.get("id"), callMap);
}
return "Success";
Code for Version 1.0 API:
leadIdLong = input.leadId.toLong();
leadIdStr = input.leadId.toString();
leadDetails = zoho.crm.getRecordById("Leads", leadIdLong);
name = ((ifnull(leadDetails.get("First Name"),"")) + " ") + ifnull(leadDetails.get("Last Name"),"");
accountmap = map();
accountmap.put(("Account Name"), name);
accountmap.put("App Out", ifnull(leadDetails.get("App Out"),""));
accountmap.put("Approved Date", ifnull(leadDetails.get("Approved Date"),""));
accountmap.put("Associated MIDs", ifnull(leadDetails.get("Associated MIDs"),""));
accountmap.put("Average Monthly Processing", ifnull(leadDetails.get("Average Monthly Processing"),""));
accountmap.put("Processing", ifnull(leadDetails.get("Processing"),""));
accountmap.put("Processing Date", ifnull(leadDetails.get("Processing Date"),""));
accountmap.put("Rate Type", ifnull(leadDetails.get("Rate Type"),""));
accountmap.put("Type of Ownership", ifnull(leadDetails.get("Type of Ownership"),""));
accountmap.put("Website", ifnull(leadDetails.get("Website"),""));
accountmap.put("Billing Code", ifnull(leadDetails.get("Zip Code"),""));
accountmap.put("Yelp", ifnull(leadDetails.get("Yelp"),""));
accountcreate = zoho.crm.create(("Accounts"), accountmap);
newacctId = (accountcreate).get("Id");
RelNotes = zoho.crm.getRelatedRecords("Notes", "Leads", leadIdStr);
countVal = 0;
for each ele in RelNotes
{
countVal = (countVal + 1);
notemap = map();
notemap.put("entityId", newacctId);
notemap.put("Note Title", ifnull(ele.get("Title")," "));
notemap.put("Note Content", ifnull(ele.get("Note Content")," "));
notecreate = zoho.crm.create("Notes", notemap);
}
TaskDetails = zoho.crm.getRelatedRecords("Tasks", "Leads", leadIdStr);
for each ele1 in TaskDetails
{
TaskId = ele1.get("ACTIVITYID");
taskMap = map();
taskMap.put("SEID", newacctId);
taskMap.put("SEMODULE", ("Accounts"));
UpdateTask = zoho.crm.updateRecord("Tasks", TaskId, taskMap);
}
EventDetails = zoho.crm.getRelatedRecords("Events", "Leads", leadIdStr);
for each ele2 in EventDetails
{
eventId = ele2.get("ACTIVITYID");
eventMap = map();
eventMap.put("SEID", newacctId);
eventMap.put("SEMODULE", ("Accounts"));
UpdateEvent = zoho.crm.updateRecord("Events", eventId, eventMap);
}
CallDetails = zoho.crm.getRelatedRecords("Calls", "Leads", leadIdStr);
for each ele3 in CallDetails
{
callId = ele3.get("ACTIVITYID");
callMap = map();
callMap.put("SEID", newacctId);
callMap.put("SEMODULE", ("Accounts"));
UpdateCall = zoho.crm.updateRecord("Calls", callId, callMap);
}
}
return "Success";
----------------------------------------------------------------------------------------------------
Note:
- The code is zoho.crm._getRecordById for Version 1.0 of APIs.
- You can further customize by creating a custom field in leads module named "Account" or "Company". You can map this custom field to the Account name to create an account with the organization's name.
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!