+ Start a Discussion
GAURAV SETHGAURAV SETH 

Adding Event through Apex Trigger showing different time on Calendar

Hi,

I am creating a event object using apex trigger

if(eightAM == 'true') {
                                                      DateTime startDateTime = kstartDate;
                                                    DateTime ednDateTime = kendDate;
                                                        Event eventObject = new Event();
                                                        eventObject.OwnerId = userID;
                                                        eventObject.Location = location; 
                                                        eventObject.StartDateTime = startDateTime.addHours(8);
                                                        eventObject.EndDateTime =startDateTime.addHours(9);
insert eventObject;
}

If it is 8 AM, I am adding 8 hours to create event entry and it is getting inserted but Calendar always show time 4 hours less.

I have check the settings , my time zone and company profile time zone is same.How can I resolve this issue ?

Thanks,
Best Answer chosen by GAURAV SETH
SidPSidP
@GAURAV SETH

Where these (kstartDate, kendDate ) are coming from? I am assuming somewhere you are creating an new instance using Datetime.now()
This method creates the current DateTime as per GMT and not the user's local timezone, that's how you see the difference in hours. You can use the following two ways to return the new instance in the user's local timezone.

1) Datetime.now().format() ==> this returns the  a local time in the String datatype
2) 
Datetime dt = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(dt);
Datetime localDT = dt.addSeconds(offset/1000);
System.debug ('local ==> ' + localDT);


You have to use convert kstartDate and kendDate into the local timezone before using them.

Mark this as answer if this resolve your issue! 

All Answers

AnudeepAnudeep (Salesforce Developers) 
Hi Gaurav, 

I read this somewhere

By default, your daily and weekly calendar views display the range of hours specified as Start of Day and End of Day on your Personal Information page.

To change the default range of hours that your calendar displays, change the Start of day and End of day fields on your personal information page. For example, if your workday typically begins at 8:00 AM and ends at 6:00 PM, changing Start of day to 8:00 AM and End of day to 6:00 PM causes your daily and weekly calendar views to default to that range of hours.

Anudeep
GAURAV SETHGAURAV SETH
Thanks Anudeep,

I changed range of hours from 8AM to 8 PM in my personal information page.
but there is a difference in event time and calendar event time.

Do we have to take care of something when creatign Event record from Apex code ?

Gaurav
SidPSidP
@GAURAV SETH

Where these (kstartDate, kendDate ) are coming from? I am assuming somewhere you are creating an new instance using Datetime.now()
This method creates the current DateTime as per GMT and not the user's local timezone, that's how you see the difference in hours. You can use the following two ways to return the new instance in the user's local timezone.

1) Datetime.now().format() ==> this returns the  a local time in the String datatype
2) 
Datetime dt = Datetime.now();
Integer offset = UserInfo.getTimezone().getOffset(dt);
Datetime localDT = dt.addSeconds(offset/1000);
System.debug ('local ==> ' + localDT);


You have to use convert kstartDate and kendDate into the local timezone before using them.

Mark this as answer if this resolve your issue! 
This was selected as the best answer
GAURAV SETHGAURAV SETH
Thanks Siddharth for your help.
Rehan Khan 34Rehan Khan 34
Thanks For Sharing This Information
https://www.hindisarkariyojana.in/