Welcome back everyone!
Business scenario:
Winning a deal is never easy. There are a lot of stuff that you need to do in order to follow through with the deal. Qualification, Analysis, Value Proposition, Identifying Decision Makers, Proposals, Negotiations, etc are the stages of a deal. And there are a lot more mini-stages which are quite necessary. For instance, scheduling a call, reviewing a proposal, scheduling a product demo, etc. Those mini-stages are documented in your CRM as Tasks, which you or your boss can assign to yourself or others.
However, the thing is, the tasks are only relevant only before the deal is either won or lost. After the deal is done and dusted, there is no need for the existing tasks. It just takes up space and a bit of your time (you might do a periodic check). As a result, although you can manually close tasks, wouldn't it be convenient to close all the tasks when the deal stage is changed to "Closed (Won)", "Closed (Lost)" or "Closed (Lost to Competition)"?
This week's tip helps you create a workflow, which triggers the function to get executed whenever the deal stage is moved to Closed.
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: "Close all tasks". Add a description(optional).
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “dealId” and select the value as “Deal Id”.
- Click Save&Execute Script to check the code.
- Save the function.
The Code:
For V2 API - DRE Editor:
relatedTask = zoho.crm.getRelatedRecords("Tasks","Deals",dealId.toLong(),1,200);
//info relatedTask;
mapVariable = Map();
for each task in relatedTask
{
taskID = ifnull(task.get("id"),"");
status = ifnull(task.get("Status"),"");
if(status != "Completed")
{
mapVariable.put("Status","Completed");
updateTask = zoho.crm.update("Tasks",taskID.toLong(),mapVariable);
info updateTask;
}
}
For V1 API - Old Editor :
relatedTask = zoho.crm.getRelatedRecords("Tasks","Potentials",dealId.toString(),1,200);
//info relatedTask;
mapVariable = Map();
for each task in relatedTask
{
taskID = ifnull(task.get("ACTIVITYID"),"");
status = ifnull(task.get("Status"),"");
if(status != "Completed")
{
mapVariable.put("Status","Completed");
updateTask = zoho.crm.update("Tasks",taskID.toString(),mapVariable);
info updateTask;
}
}
Note:
Include this function in a Workflow Rule, with the conditions as executed whenever the field Stage is updated, Stage is Closed Won, Closed-Lost or Closed-Lost to Competition.
The code is zoho.crm._getRelatedRecords for Version 1.0 of 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!