For this tutorial in video form, click here
In this article, we’ll talk about how to deal with timezones and date times within Zoho CRM. It’s a really tricky topic. To give it a concrete application, let’s set up a real world scenario: let’s say that you have a field in your CRM called “appointment start”, and you want to automatically set “appointment finish”.
We’ve got a custom function here:
It gets the data from the lead (the appointment start), and then correctly sets the appointment finish time to be an hour later.
You might think that’s a trivial task. Shouldn’t something so simple require only a line or two of code? You might think something like this could get the job done:
While the function technically succeeds, the response we get just says “Invalid Data”.
Zoho CRM has a different expectation than Deluge Script as to what a date time looks like. Zoho wants it to be like this:
It’s quite difficult to get it working with the timezone (highlighted in blue). You can try giving it the format it expects, but that lacks a timezone, giving us the same “Invalid Data” response.
In many timezones, the offset from GMT changes based on Daylight saving time. Furthermore, if we’re writing a generic function to use in an extension, we have no way of knowing what the offset should be. Thus, we need a way to be able to get it in the right format, regardless of what we know about the organization (which might be nothing). Zoho has helped us to come up with a method for this.
First, you do a sub-string to get the last six characters of the time that you are going to base it off.
This is because the last six characters will always be in the same format: a plus or a minus, followed by two numbers, followed by a colon, and ending with 2 more numbers. This is done in 24 hour time, which means it works universally.
Once we have the timezone (the offset of it to GMT), we can put it into a time specifying what the timezone is, and set the finish time to being the start time with an hour added. We then convert it to a string using this format here:
It’s year, month, day, hour, minute, second, followed by the timezone offset at the end. The use of single apostrophes allow us to use symbols that aren’t normally part of it. For example, the letter T has a pair of single apostrophes around it because that’s not a valid time formatting symbol. We also have single quotes around the GMT offset for the same reason.
This is a bit complicated for the result you’re getting, but it’s better than nothing. The code used is:
start_time_iso_string = lead_record.get("Appointment_start"); timezone = start_time_iso_string.subText(start_time_iso_string.length() - 6); start_time = start_time_iso_string.toTime("yyyy-MM-dd'T'HH:mm:ss", timezone); ZOHO_CRM_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'" + timezone + "'"; start_time_str = start_time.toString(ZOHO_CRM_TIME_FORMAT, timezone); finish_time_str = start_time.addHour(1).toString(ZOHO_CRM_TIME_FORMAT, timezone);