For Zoho Services only:


I'm actually part of a bigger team at Ascent Business Solutions where we have support technicians and project consultants. Support is for smaller technical fixes but this can include developments, reports or integrations; depending on the size of the task. Projects are for more time-consuming developments such as revamps of the Zoho Suite of apps or on-site training. The advantage of a team is that if I am out-of-office for a day or so, there is always someone at Ascent Business Solutions who can deal with any queries/issues you may have.

Our support rates can be found and purchased at http://ascentbusiness.co.uk/zoho-support-2. A support bundle doesn't have an expiry date. So whether we can do what you want within the bundle and a year later need further support, if there are minutes left on the bundle then there is no additional charge.

Our project rates for bigger developments can be found at http://ascentbusiness.co.uk/crm-solutions/zoho-crm-packages-prices and will involve a dedicated project consultant along with developers who will hold your hand through the development process.

If you want help building a solution for one of the Zoho Apps in the Zoho Suite, contact us on 0121 293 8140 (UK) or by email at info@ascentbusiness.co.uk. You can also visit our website at http://ascentbusiness.co.uk.

I regularly build and specialize in 2-way API integrations for Xero, Shopify and eBay.

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:
  • ZohoCRM.modules.ALL
  • ZohoCRM.users.READ
  • ZohoCRM.coql.READ
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

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 bc1qjtp4l4ra452wzvuk9a45yfj82zkahsyy2z379y
© 2022 Joel Lipman .com. All Rights Reserved.