I'm trying to create an event for another user which needs to appear on their Calendar (in that user's Timezone).
e.g. Current user's Timezone is Australia/Sydney, I need to create an event for a New Zealand user with timezone Pacific/Auckland.
When you simply create an event below it's getting saved in current user's Timezone.
DateTime eventStart = Datetime.newInstance(2019, 11, 14, 10, 0, 0); DateTime eventEnd = Datetime.newInstance(2019, 11, 14, 10, 30, 0); insert new Event(OwnerId = userIdwithDifferentTimezone,StartDateTime = eventStart, EndDateTime = eventEnd, Subject = subject);
I tried to assign Timezone by converting DateTime to that Timezone and add suffix as below,
DateTime dt = eventStart; String targetTZ = dt.format('Z',tzName); // Get TZ offset for target // Set the timezone String dt_str = '"'+dt.year()+'-'+dt.month()+'-'+dt.day()+'T'+dt.hour()+':'+dt.minute()+':'+dt.second()+'.000'+targetTZ+'"' ; eventStart = (Datetime)JSON.deserialize(dt_str, Datetime.class);
But it seems this does not setting the Timezone, when DateTime object is initialized, seems it's assigning current user's Timezone instead.
What is the correct way to set Timezone in Event?
Appreciate any help, thoughts to correct this. Thank you!