Print

Zoho CRM: Schedule a Task: Timesheet Reminder

What?
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:
copyraw
//
// 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;
			}
		}
	}
}
  1.  // 
  2.  // set to the number of hours minus the ones to be logged on Friday (40 - 8) 
  3.  v_RemindeMeAtHoursLogged = 32
  4.  // 
  5.  // set to the Account record of our organization 
  6.  v_OrgAccountRecord = 123456789012345678
  7.  // 
  8.  // loop through all users 
  9.  r_Users = zoho.crm.getRecords("users")
  10.  if(!isnull(r_Users.get("users"))) 
  11.  { 
  12.      for each  r_User in r_Users.get("users") 
  13.      { 
  14.          // loop through active users 
  15.          if(r_User.get("status") == "active") 
  16.          { 
  17.              v_Email = ifnull(r_User.get("email"),"")
  18.              v_LastName = ifnull(r_User.get("last_name"),"")
  19.              // 
  20.              // single out this person (can be removed after testing) 
  21.              if(v_LastName.containsIgnoreCase("Lipman")) 
  22.              { 
  23.                  // 
  24.                  // get user id 
  25.                  v_UserID = r_User.get("id")
  26.                  // 
  27.                  // startofweek is a sunday but we want to start counting from Monday 00:00 
  28.                  v_FirstDateOfWeek = zoho.currentdate.toStartOfWeek().addDay(1).toString("yyyy-MM-dd")
  29.                  // 
  30.                  // build up COQL Query 
  31.                  v_CoqlQuery = "select Start_DateTime, End_DateTime from Events where Created_By='" + v_UserID + "' and ("
  32.                  v_CoqlQuery = v_CoqlQuery + "Start_DateTime>='" + v_FirstDateOfWeek + "T00:00:00+00:00' and "
  33.                  v_CoqlQuery = v_CoqlQuery + "End_DateTime<='" + zoho.currentdate.toString("yyyy-MM-dd") + "T00:00:00+00:00') order by Start_DateTime"
  34.                  // 
  35.                  // send request to CRM API 
  36.                  m_Params = Map()
  37.                  m_Params.put("select_query",v_CoqlQuery)
  38.                  r_LoggedEvents = invokeUrl 
  39.                  [ 
  40.                      url :"https://www.zohoapis.eu/crm/v2/coql" 
  41.                      type :POST 
  42.                      parameters:m_Params.toString() 
  43.                      connection:"joel_timesheet" 
  44.                  ]
  45.                  // 
  46.                  // now loop through returned logged events and calculate total time (counting in milliseconds) 
  47.                  v_TotalSeconds = 0
  48.                  if(!isnull(r_LoggedEvents.get("data"))) 
  49.                  { 
  50.                      for each  r_Data in r_LoggedEvents.get("data") 
  51.                      { 
  52.                          v_StartTime = r_Data.get("Start_DateTime").subString(0,10) + " " + r_Data.get("Start_DateTime").subString(11,16)
  53.                          v_EndTime = r_Data.get("End_DateTime").subString(0,10) + " " + r_Data.get("End_DateTime").subString(11,16)
  54.                          // 
  55.                          // convert to seconds 
  56.                          v_NowEpoch = v_StartTime.toTime().toLong()
  57.                          v_NextEpoch = v_EndTime.toTime().toLong()
  58.                          v_UnixSeconds = (v_NextEpoch - v_NowEpoch) / 1000
  59.                          v_TotalSeconds = v_TotalSeconds + v_UnixSeconds; 
  60.                      } 
  61.                  } 
  62.                  // 
  63.                  // convert to hours:minutes 
  64.                  v_Hours = floor((v_TotalSeconds / 60) / 60)
  65.                  v_RemainingSeconds = v_TotalSeconds - v_Hours * 60 * 60
  66.                  v_Minutes = floor(v_RemainingSeconds / 60)
  67.                  // 
  68.                  // if hours is less than expected, create a task 
  69.                  if(v_Hours < v_RemindeMeAtHoursLogged) 
  70.                  { 
  71.                      m_CreateTask = Map()
  72.                      m_CreateTask.put("$se_module","Accounts")
  73.                      m_CreateTask.put("Owner",v_UserID)
  74.                      // 
  75.                      // search for a contact record with this users email (for the task - doesn't matter if blank) 
  76.                      v_CrmContactID = 0
  77.                      l_Search = zoho.crm.searchRecords("Contacts","Email:equals:" + v_Email)
  78.                      for each  r_Result in l_Search 
  79.                      { 
  80.                          if(!isnull(r_Result.get("Email"))) 
  81.                          { 
  82.                              if(r_Result.get("Email").equalsIgnoreCase(v_Email)) 
  83.                              { 
  84.                                  v_CrmContactID = r_Result.get("id").toLong()
  85.                              } 
  86.                          } 
  87.                      } 
  88.                      if(v_CrmContactID != 0) 
  89.                      { 
  90.                          m_CreateTask.put("Who_Id",v_CrmContactID)
  91.                      } 
  92.                      // 
  93.                      // set the what_id to the account record of our organization 
  94.                      m_CreateTask.put("What_Id",v_OrgAccountRecord)
  95.                      m_CreateTask.put("Subject","Timesheet Reminder: Log 40 Hours this Week")
  96.                      // 
  97.                      // if this function was run before Friday, get Friday's date 
  98.                      v_FridayDate = zoho.currentdate.toStartOfWeek().addDay(5).toString("yyyy-MM-dd")
  99.                      m_CreateTask.put("Due_Date",v_FridayDate)
  100.                      m_CreateTask.put("Status","Not Started")
  101.                      // 
  102.                      // create a nice description 
  103.                      v_GrammarHour = if(v_Hours == 1,"","s")
  104.                      v_DisplayLogTime = v_Hours + " hour" + v_GrammarHour; 
  105.                      if(v_Minutes > 0) 
  106.                      { 
  107.                          v_GrammarMins = if(v_Minutes == 1,"","s")
  108.                          v_DisplayLogTime = v_DisplayLogTime + " and " + v_Minutes + " minute" + v_GrammarMins; 
  109.                      } 
  110.                      v_Description = "This is your friendly reminder that you've only logged " + v_DisplayLogTime + " this week.  "
  111.                      v_Description = v_Description + "Please try to log your events daily so that you don't forget what you were doing."
  112.                      m_CreateTask.put("Description",v_Description)
  113.                      // 
  114.                      // set a reminder for 4pm this Friday 
  115.                      v_ReminderStr = "FREQ=NONE;ACTION=POPUP;TRIGGER=DATE-TIME:" + v_FridayDate.toString("yyyy-MM-dd'T'16:00:00") + "+00:00"
  116.                      m_ReminderAlarm = Map()
  117.                      m_ReminderAlarm.put("ALARM",v_ReminderStr)
  118.                      m_CreateTask.put("Remind_At",m_ReminderAlarm)
  119.                      // 
  120.                      // display request to admin 
  121.                      info m_CreateTask; 
  122.                      // 
  123.                      // create task 
  124.                      r_CreateTask = zoho.crm.createRecord("Tasks",m_CreateTask)
  125.                      info r_CreateTask; 
  126.                  } 
  127.                  // 
  128.                  // break to check this "Lipman" user but remove after testing 
  129.                  break
  130.              } 
  131.          } 
  132.      } 
  133.  } 

Category: Zoho :: Article: 789