Welcome back everyone!
Business scenario:
Tasks in Zoho CRM helps organize your to-do-lists efficiently. It not only helps keep a tab on your to-do-lists, but also enables easy access to related record in other modules. This for sure, makes you productive. What if I say there is a possibility of taking this one step ahead? While Tasks enables easy access to related records associated with the task records, won’t it be great if it were to fetch all the required information from the related record modules to complete the task?
Such a possibility cuts down on the need to loop between the task record and the related record module - say for example between the lead record and the associated task record. This week’s custom function helps you do just that. It auto-updates the custom fields in task records with required details from the related record modules.
Pre-requisites:
- As custom functions are module specific, you need to update the appropriate custom function individually for the respective modules that require the auto-update functionality. We have covered four use cases with certain important fields in the below code. For instance, the code shared for Leads module auto-updates the State and Phone number details from the Leads module on the task record's custom fields. Choose the ones that fits your sales process and update the required field parameters accordingly.
- Add the required custom fields in the Tasks Layouts. Go to Setup > Customization > Module and Fields > Tasks > Layouts , drag and drop the required custom fields. The details fetched from the respective record modules gets updated in these custom fields.
Getting started with the custom function:
- Go to Setup > Automation > Actions > Custom Functions > Configure Custom Function > Write your own .
- Provide a name for the button. For example: “Auto update tasks.”. Add a description(optional).
- Choose the module as " Tasks ".
- Click “ Free flow scripting ”.
- Copy the code given below.
- Click “ Edit arguments ”.
- Enter the name as “ taskId ” and select the value as “ Task Id ”.
- Save the changes.
The script:
Code for Version 2.0 API:
taskDetails = zoho.crm.getRecordById("Tasks", input.taskId.toLong());
//info taskDetails;
whatid = ifnull(taskDetails.get("What_Id"),"");
whoid = ifnull(taskDetails.get("Who_Id"),"");
if( whatid != "")
{
recordid = ifnull(taskDetails.get("What_Id"),"").get("id")''
module = ifnull(taskDetails.get("se_module"),"");
getDetails = zoho.crm.getRecordById(module, whatid);
timezone = ifnull(getDetails.get("Time_Zone"),"");
mp=map();
mp.put("Time_Zone",timezone);
update = zoho.crm.update("Tasks", taskId, mp);
info mp;
info update;
}
if( whoid != "")
{
contid = ifnull(taskDetails.get("Who_Id"),"").get("id");
getDetails = zoho.crm.getRecordById("Contacts", contid);
timezone = ifnull(getDetails.get("Time_Zone"),"");
mp=map();
mp.put("Time_Zone",timezone);
update = zoho.crm.update("Tasks", taskId, mp);
info mp;
info update;
}
Code for Version 1.0 API:
1) Update Tasks from Leads :
taskIdStr=input.taskId.toString();
taskDetails = zoho.crm.getRecordById("Tasks", input.taskId);
module = ifnull(taskDetails.get("SEMODULE"),"");
id = ifnull(taskDetails.get("RELATEDTOID"),"");
if ( module == "Leads")
{
leadDetails = zoho.crm.getRecordById(("Leads"), id.toLong());
mp=map();
mp.put("State",ifnull(leadDetails.get("State"),""));
mp.put("Phone",ifnull(leadDetails.get("Phone"),""));
update=zoho.crm.updateRecord("Tasks", taskIdStr, mp);
info mp;
info update;
}
2) Update Tasks from Contacts :
taskIdStr = input.taskId.toString();
taskDetails = zoho.crm.getRecordById("Tasks", input.taskId);
contid = ifnull(taskDetails.get("CONTACTID"),"");
if ( contid != "")
{
contDetails = zoho.crm.getRecordById("Contacts", contid.toLong());
contmap = map();
contmap.put("Title", ifnull(contDetails.get("Title"),""));
contupdate = zoho.crm.updateRecord("Tasks", taskIdStr, contmap);
info contmap;
info contupdate;
}
3) Update Tasks from Solutions :
taskIdStr = input.taskId.toString();
taskdetails = zoho.crm.getRecordById("Tasks", input.taskId);
module = ifnull(taskdetails.get("SEMODULE"),"");
id = ifnull(taskdetails.get("RELATEDTOID"),"");
if ( module == "Solutions")
{
solutionsDetails = zoho.crm.getRecordById(("Solutions"), id.toLong());
mp=map();
mp.put("Subject",ifnull(solutionsDetails.get("Solution Title"),""));
mp.put("SMOWNERID",ifnull(solutionsDetails.get("SMOWNERID"),""));
mp.put("Description",ifnull(solutionsDetails.get("Additional Information"),""));
mp.put("Due Date", ifnull(solutionsDetails.get("Deadline Date"),""));
update=zoho.crm.updateRecord("Tasks", taskIdStr, mp);
}
4) Update Tasks from Accounts :
taskIdStr = input.taskId.toString();
taskDetails = zoho.crm.getRecordById("Tasks", input.taskId);
module = ifnull(taskDetails.get("SEMODULE"),"");
id = ifnull(taskDetails.get("RELATEDTOID"),"");
if ( module == "Accounts")
{
acctDetails = zoho.crm.getRecordById("Accounts", id.toLong());
acctmap = map();
acctmap.put("SQFT", ifnull(acctDetails.get("SQFT"),""));
acctmap.put("Lease Commencement", ifnull(acctDetails.get("Lease Commencement"),""));
acctmap.put("Termination Date", ifnull(acctDetails.get("Termination Date"),""));
acctmap.put("Lease Expiration", ifnull(acctDetails.get("Lease Expiration"),""));
acctupdate = zoho.crm.updateRecord("Tasks", taskIdStr, acctmap);
info acctmap;
info acctupdate;
}