Welcome back everyone!
Last week, we learnt how to update information between two modules via lookup fields. This week, let's look at a custom function that creates activities automatically based on specific criteria.
Business scenario:
The Workflow Rules feature takes care of most of the automation requirements in Zoho CRM. Some of you might be wondering why is there a need to rely on custom functions, when workflows, blueprints or schedules helps meet most of your requirements. While this is largely true, there are a few requirements which could not be met out-of-the-box using workflows.
Let's take the Activity module for example. It comprises of Tasks, Events and Calls. However, only Tasks can be created upon meeting workflow conditions, whereas Calls and Events cannot be. Sure, you can make the task description as "Make a call to the customer at 5PM in 2 days.". But won't you prefer it to be a lot simpler and productive to automatically create and schedule a Call record in the Activities module? This week's custom function helps you do just that. It allows you to create or schedule Calls and Events for a record through workflows. Include the custom function in the workflow to create Calls and Events that could be triggered either instantly on creation or while editing the records.
Getting started with the custom function:
Scenario
: Create follow-up activities for a prospect based on deal stage.
- Go to Setup > Automations > Actions > Custom Functions > Configure Custom Function > Write your own.
- Provide a name for the custom function. For example: “Deal Follow-up call”. Add a description(optional).
- Select the module as Deal. Add a description (optional).
- Click “Free flow scripting”.
- Copy the code given below.
- Click “Edit arguments”.
- Enter the name as “deaId” and select the value as “Deal Id”.
- Save the changes.
The script:
Code for Version 2.0 API:
For tasks :
dealDetails = zoho.crm.getRecordById("Deals",input.dealId.toLong());
taskSubject = "Créer BAL pour " + ifnull(dealDetails.get("Account_Name"),"").get("name");
mp=map();
mp.put("Subject",taskSubject);
mp.put("Due_Date",today.toDate());
mp.put("Owner",ifnull(dealDetails.get("Owner"),"").get("id"));
mp.put("What_Id",input.dealId);
mp.put("$se_module","Deals");
mp.put("Stage","Non commencé");
createResp = zoho.crm.create("Tasks",mp);
info mp;
info createResp;
For events :
DealDetails = zoho.crm.getRecordById("Deals", input.dealId);
//info DealDetails;
startdate = ifnull(DealDetails.get("Contract_Start_Date"),"").toString("yyyy-MM-dd");
enddate = ifnull(DealDetails.get("Contract_End_Date"),"").toString("yyyy-MM-dd");
eventmap =map();
eventmap.put("Event_Title",ifnull(DealDetails.get("Deal_Name"),""));
eventmap.put("Who_Id",ifnull(DealDetails.get("Contact_Name"),"").get("id"));
eventmap.put("Owner",ifnull(DealDetails.get("Owner"),"").get("id"));
eventmap.put("What_Id",input.dealId);
eventmap.put("$se_module","Deals");
eventmap.put("Start_DateTime",startdate+"T09:00:00+05:30");
eventmap.put("End_DateTime",enddate+"T10:00:00+05:30");
create1 = zoho.crm.create("Events", eventmap);
info eventmap;
info create1;
For calls :
dealDetails = zoho.crm.getRecordById("Deals", input.dealId);
info ifnull(dealDetails.get("Modified_Time"),"");
modifiedtime = ifnull(dealDetails.get("Modified_Time"),"").toTime("yyyy-MM-dd'T'HH:mm:ss").addDay(1);
mp = map();
mp.put("Subject", "Cold Call");
mp.put("Owner", ifnull(dealDetails.get("Owner"),"").get("id"));
mp.put("Call_Type", "Outbound");
mp.put("se_module", "Deals");
mp.put("What_Id", input.dealId);
mp.put("Call_Start_Time", modifiedtime.toString("yyyy-MM-dd'T'HH:mm:ss+05:30"));
mp.put("which_call", "ScheduleCall");
create = zoho.crm.create("Calls", mp);
info mp;
info create;
Code for Version 1.0 API:
For Tasks:
dealDetails = zoho.crm.getRecordById("Potentials",input.dealId);
mp=map();
mp.put("Subject","No VM Call");
mp.put("Due Date",zoho.currentdate);
mp.put("SMOWNERID",ifnull(dealDetails.get("SMOWNERID"),""));
mp.put("State",ifnull(dealDetails.get("State"),""));
mp.put("Mobile",ifnull(dealDetails.get("Mobile"),""));
mp.put("SEID",input.dealId);
mp.put("SEMODULE","Potentials");
createResp = zoho.crm.create("Tasks",mp);
info createResp;
For Events:
dealDetails = zoho.crm.getRecordById("Potentials", input.dealId);
name = ifnull(dealDetails.get("First Name"),"") + " " + ifnull(dealDetails.get("Last Name"),"") ;
start = ifnull(dealDetails.get("Event Start date"),"").toTime().toString("yyyy-MM-dd 09:00:00");
end = ifnull(dealDetails.get("Event End date"),"").toTime().toString("yyyy-MM-dd 10:00:00");
eventMap = map();
eventMap.put("Subject", name);
eventMap.put("SMOWNERID", ifnull(dealDetails.get("SMOWNERID"),""));
eventMap.put("Start DateTime", start);
eventMap.put("End DateTime", end);
eventMap.put("SEID", input.dealId);
eventMap.put("SEMODULE", "Potentials");
createEvent = zoho.crm.create("Events", eventMap);
info createEvent;
For Calls:
dealDetails = zoho.crm.getRecordById("Potentials", input.dealId);
name = ifnull(dealDetails.get("First Name"),"") + " " + ifnull(dealDetails.get("Last Name"),"") + " - Call ";
created = ifnull(dealDetails.get("Created Time"),"");
time = (created.toDate().addDay(3)).toString("yyyy-MM-dd 10:00:00");
mp = map();
mp.put("Subject", name);
mp.put("Call Type", "Outbound");
mp.put("SEMODULE", "Potentials");
mp.put("SMOWNERID", ifnull(dealDetails.get("SMOWNERID"),""));
mp.put("SEID", input.dealId);
mp.put("Call Start Time", time);
mp.put("whichCall", "ScheduleCall");
create = zoho.crm.create("Calls", mp);
info create;
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 "On a record action".
- Select "Create or Edit" and click on Next.
- 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 custom function you just added and click on Configure.
- Click on "Save and Associate".
- Save the workflow.
Note:
- You can use the code on any module. Change the name of the module from 'Deals' to whichever module you prefer and update the code accordingly.
- To schedule the action to be performed after a specific period of time from which the condition is met, select "Scheduled Actions" instead of "Instant Actions".
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.