A quick article to demonstrate code that creates a task in CRM based on the time logged against an Event/Meeting.
Why?
As developers, we're keep account of our time and we are currently logging time in our CRM. We're meant to do 40 hours, not just for Management, but for deducting from project and support bundles.
How?
The following snippet of code is on a CRM schedule set to run every Friday at 08:00 in the morning and will create a task if time logged is too low and will remind via Popup at about 4pm (16:00). You will need a connector (I've called mine joel_timesheet) and I've given it the following scopes:
- ZohoCRM.modules.ALL
- ZohoCRM.users.READ
- ZohoCRM.coql.READ
// // set to the number of hours minus the ones to be logged on Friday (40 - 8) v_RemindeMeAtHoursLogged = 32; // // set to the Account record of our organization v_OrgAccountRecord = 123456789012345678; // // loop through all users r_Users = zoho.crm.getRecords("users"); if(!isnull(r_Users.get("users"))) { for each r_User in r_Users.get("users") { // loop through active users if(r_User.get("status") == "active") { v_Email = ifnull(r_User.get("email"),""); v_LastName = ifnull(r_User.get("last_name"),""); // // single out this person (can be removed after testing) if(v_LastName.containsIgnoreCase("Lipman")) { // // get user id v_UserID = r_User.get("id"); // // startofweek is a sunday but we want to start counting from Monday 00:00 v_FirstDateOfWeek = zoho.currentdate.toStartOfWeek().addDay(1).toString("yyyy-MM-dd"); // // build up COQL Query v_CoqlQuery = "select Start_DateTime, End_DateTime from Events where Created_By='" + v_UserID + "' and ("; v_CoqlQuery = v_CoqlQuery + "Start_DateTime>='" + v_FirstDateOfWeek + "T00:00:00+00:00' and "; v_CoqlQuery = v_CoqlQuery + "End_DateTime<='" + zoho.currentdate.toString("yyyy-MM-dd") + "T00:00:00+00:00') order by Start_DateTime"; // // send request to CRM API m_Params = Map(); m_Params.put("select_query",v_CoqlQuery); r_LoggedEvents = invokeurl [ url :"https://www.zohoapis.eu/crm/v2/coql" type :POST parameters:m_Params.toString() connection:"joel_timesheet" ]; // // now loop through returned logged events and calculate total time (counting in milliseconds) v_TotalSeconds = 0; if(!isnull(r_LoggedEvents.get("data"))) { for each r_Data in r_LoggedEvents.get("data") { v_StartTime = r_Data.get("Start_DateTime").subString(0,10) + " " + r_Data.get("Start_DateTime").subString(11,16); v_EndTime = r_Data.get("End_DateTime").subString(0,10) + " " + r_Data.get("End_DateTime").subString(11,16); // // convert to seconds v_NowEpoch = v_StartTime.toTime().toLong(); v_NextEpoch = v_EndTime.toTime().toLong(); v_UnixSeconds = (v_NextEpoch - v_NowEpoch) / 1000; v_TotalSeconds = v_TotalSeconds + v_UnixSeconds; } } // // convert to hours:minutes v_Hours = floor((v_TotalSeconds / 60) / 60); v_RemainingSeconds = v_TotalSeconds - v_Hours * 60 * 60; v_Minutes = floor(v_RemainingSeconds / 60); // // if hours is less than expected, create a task if(v_Hours < v_RemindeMeAtHoursLogged) { m_CreateTask = Map(); m_CreateTask.put("$se_module","Accounts"); m_CreateTask.put("Owner",v_UserID); // // search for a contact record with this users email (for the task - doesn't matter if blank) v_CrmContactID = 0; l_Search = zoho.crm.searchRecords("Contacts","Email:equals:" + v_Email); for each r_Result in l_Search { if(!isnull(r_Result.get("Email"))) { if(r_Result.get("Email").equalsIgnoreCase(v_Email)) { v_CrmContactID = r_Result.get("id").toLong(); } } } if(v_CrmContactID != 0) { m_CreateTask.put("Who_Id",v_CrmContactID); } // // set the what_id to the account record of our organization m_CreateTask.put("What_Id",v_OrgAccountRecord); m_CreateTask.put("Subject","Timesheet Reminder: Log 40 Hours this Week"); // // if this function was run before Friday, get Friday's date v_FridayDate = zoho.currentdate.toStartOfWeek().addDay(5).toString("yyyy-MM-dd"); m_CreateTask.put("Due_Date",v_FridayDate); m_CreateTask.put("Status","Not Started"); // // create a nice description v_GrammarHour = if(v_Hours == 1,"","s"); v_DisplayLogTime = v_Hours + " hour" + v_GrammarHour; if(v_Minutes > 0) { v_GrammarMins = if(v_Minutes == 1,"","s"); v_DisplayLogTime = v_DisplayLogTime + " and " + v_Minutes + " minute" + v_GrammarMins; } v_Description = "This is your friendly reminder that you've only logged " + v_DisplayLogTime + " this week. "; v_Description = v_Description + "Please try to log your events daily so that you don't forget what you were doing."; m_CreateTask.put("Description",v_Description); // // set a reminder for 4pm this Friday v_ReminderStr = "FREQ=NONE;ACTION=POPUP;TRIGGER=DATE-TIME:" + v_FridayDate.toString("yyyy-MM-dd'T'16:00:00") + "+00:00"; m_ReminderAlarm = Map(); m_ReminderAlarm.put("ALARM",v_ReminderStr); m_CreateTask.put("Remind_At",m_ReminderAlarm); // // display request to admin info m_CreateTask; // // create task r_CreateTask = zoho.crm.createRecord("Tasks",m_CreateTask); info r_CreateTask; } // // break to check this "Lipman" user but remove after testing break; } } } }
- //
- // set to the number of hours minus the ones to be logged on Friday (40 - 8)
- v_RemindeMeAtHoursLogged = 32;
- //
- // set to the Account record of our organization
- v_OrgAccountRecord = 123456789012345678;
- //
- // loop through all users
- r_Users = zoho.crm.getRecords("users");
- if(!isnull(r_Users.get("users")))
- {
- for each r_User in r_Users.get("users")
- {
- // loop through active users
- if(r_User.get("status") == "active")
- {
- v_Email = ifnull(r_User.get("email"),"");
- v_LastName = ifnull(r_User.get("last_name"),"");
- //
- // single out this person (can be removed after testing)
- if(v_LastName.containsIgnoreCase("Lipman"))
- {
- //
- // get user id
- v_UserID = r_User.get("id");
- //
- // startofweek is a sunday but we want to start counting from Monday 00:00
- v_FirstDateOfWeek = zoho.currentdate.toStartOfWeek().addDay(1).toString("yyyy-MM-dd");
- //
- // build up COQL Query
- v_CoqlQuery = "select Start_DateTime, End_DateTime from Events where Created_By='" + v_UserID + "' and (";
- v_CoqlQuery = v_CoqlQuery + "Start_DateTime>='" + v_FirstDateOfWeek + "T00:00:00+00:00' and ";
- v_CoqlQuery = v_CoqlQuery + "End_DateTime<='" + zoho.currentdate.toString("yyyy-MM-dd") + "T00:00:00+00:00') order by Start_DateTime";
- //
- // send request to CRM API
- m_Params = Map();
- m_Params.put("select_query",v_CoqlQuery);
- r_LoggedEvents = invokeUrl
- [
- url :"https://www.zohoapis.eu/crm/v2/coql"
- type :POST
- parameters:m_Params.toString()
- connection:"joel_timesheet"
- ];
- //
- // now loop through returned logged events and calculate total time (counting in milliseconds)
- v_TotalSeconds = 0;
- if(!isnull(r_LoggedEvents.get("data")))
- {
- for each r_Data in r_LoggedEvents.get("data")
- {
- v_StartTime = r_Data.get("Start_DateTime").subString(0,10) + " " + r_Data.get("Start_DateTime").subString(11,16);
- v_EndTime = r_Data.get("End_DateTime").subString(0,10) + " " + r_Data.get("End_DateTime").subString(11,16);
- //
- // convert to seconds
- v_NowEpoch = v_StartTime.toTime().toLong();
- v_NextEpoch = v_EndTime.toTime().toLong();
- v_UnixSeconds = (v_NextEpoch - v_NowEpoch) / 1000;
- v_TotalSeconds = v_TotalSeconds + v_UnixSeconds;
- }
- }
- //
- // convert to hours:minutes
- v_Hours = floor((v_TotalSeconds / 60) / 60);
- v_RemainingSeconds = v_TotalSeconds - v_Hours * 60 * 60;
- v_Minutes = floor(v_RemainingSeconds / 60);
- //
- // if hours is less than expected, create a task
- if(v_Hours < v_RemindeMeAtHoursLogged)
- {
- m_CreateTask = Map();
- m_CreateTask.put("$se_module","Accounts");
- m_CreateTask.put("Owner",v_UserID);
- //
- // search for a contact record with this users email (for the task - doesn't matter if blank)
- v_CrmContactID = 0;
- l_Search = zoho.crm.searchRecords("Contacts","Email:equals:" + v_Email);
- for each r_Result in l_Search
- {
- if(!isnull(r_Result.get("Email")))
- {
- if(r_Result.get("Email").equalsIgnoreCase(v_Email))
- {
- v_CrmContactID = r_Result.get("id").toLong();
- }
- }
- }
- if(v_CrmContactID != 0)
- {
- m_CreateTask.put("Who_Id",v_CrmContactID);
- }
- //
- // set the what_id to the account record of our organization
- m_CreateTask.put("What_Id",v_OrgAccountRecord);
- m_CreateTask.put("Subject","Timesheet Reminder: Log 40 Hours this Week");
- //
- // if this function was run before Friday, get Friday's date
- v_FridayDate = zoho.currentdate.toStartOfWeek().addDay(5).toString("yyyy-MM-dd");
- m_CreateTask.put("Due_Date",v_FridayDate);
- m_CreateTask.put("Status","Not Started");
- //
- // create a nice description
- v_GrammarHour = if(v_Hours == 1,"","s");
- v_DisplayLogTime = v_Hours + " hour" + v_GrammarHour;
- if(v_Minutes > 0)
- {
- v_GrammarMins = if(v_Minutes == 1,"","s");
- v_DisplayLogTime = v_DisplayLogTime + " and " + v_Minutes + " minute" + v_GrammarMins;
- }
- v_Description = "This is your friendly reminder that you've only logged " + v_DisplayLogTime + " this week. ";
- v_Description = v_Description + "Please try to log your events daily so that you don't forget what you were doing.";
- m_CreateTask.put("Description",v_Description);
- //
- // set a reminder for 4pm this Friday
- v_ReminderStr = "FREQ=NONE;ACTION=POPUP;TRIGGER=DATE-TIME:" + v_FridayDate.toString("yyyy-MM-dd'T'16:00:00") + "+00:00";
- m_ReminderAlarm = Map();
- m_ReminderAlarm.put("ALARM",v_ReminderStr);
- m_CreateTask.put("Remind_At",m_ReminderAlarm);
- //
- // display request to admin
- info m_CreateTask;
- //
- // create task
- r_CreateTask = zoho.crm.createRecord("Tasks",m_CreateTask);
- info r_CreateTask;
- }
- //
- // break to check this "Lipman" user but remove after testing
- break;
- }
- }
- }
- }