function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
CommaSpaceCommaSpace 

Updating Event time from GMT/UTC to Local Time & account for daylight savings

Hello! I'm trying to use an apex trigger to update the starttime of an event.  I have a visualflow intake form that creates an event on a future date based on the information provided. The event is created in GMT and after scouring the interwebs and piecing code/ideas together I think I am making progress, but now I'm getting an error Line 20: Variable does not exist: time.
Would greatly appreciate if you'd take a look and provide input/assistance. 
Best,
Ruth
 
trigger FindTime on Event (before insert) {
//If type is "call" Pull event startdatetime
//Pull month from startdatetime

//Event Months during daylight savings add 6hrs
//Event Month after daylight savings add 5 hrs
//Event Months before daylight savings add 5 hrs

//Event Month of March, check which week event date falls in and add 6 hrs if in or after the 3rd week of the month; otherwise add 5hrs.
//Event Month of November, check which week event date falls in and add 5 hrs if in or after the 2rd week of the month; otherwise add 6hrs.


//Setting up trigger info
    Public Date mydate{get;set;}
    Public Time mytime{get;set;}
    for (Event id: trigger.new){
        if(id.type =='Call'){
            DateTime dT = id.startdatetime;
            myDate = date.newinstance(dT.year(), dT.month(), dT.day());
            mytime = time.newinstance(dT.hour(), dT.minute(), dT.second());
            Integer WeekOfMonth = Math.ceil((Double)(id.startdatetime().Day()) / 7).intValue();               

//if Month is greater than 3 and less than 11
            If(dt.month() >3){
                IF(dt.month()<11){
                    dt.addHours(6);
                }
                //following code is if month = 12 (because the 2nd if above returned false)
                dt.addHours(5);
                // following code is when the first IF returns false
                dt.addhours(5);
            } //closing bracket on first set of if's

//starting IF set for dates in March
            else If (dt.month = 3){
                If(weekofmonth >= 3){
                    //if week of month is greater than or equal to 3 (meaning into the 3rd week, meaning after the 2nd weekend) then DST is in effect; add 6 hrs
                    dt.addHours(6);
                    dt.addHours(5);
                    
                }}
//starting Else set for dates in November
            Else //last option is for event to be in november
                If (weekofmonth >= 2){
                 //meaning it's in the 2nd week or after the first weekend
                 dt.addHours(5);
                 dt.addHours(6);
                }}}}

 
Saurabh Sagar GulatiSaurabh Sagar Gulati
You are getting error on Line 20 because Time.NewInstance has 4 parameters but you have used only 3. If you dont want to use the 4th parameter mention it as 0.
CommaSpaceCommaSpace
Hi Saurabh, Thank you! That helped and then I figured out a few more things and have the updated code as shown below.  Now, interestingly, the trigger passes the test class but it doesn't actually update the event record.  Any suggestions?
trigger FindTime on Event (before insert, before update) {
    //Setting up trigger info
    Public Date mydate{get;set;}
    Public Time mytime{get;set;}
    for (Event id: trigger.new){
        system.debug('trigger'+trigger.new);
        if(id.type =='Call'){
            //If type is "call" Pull event startdatetime
            system.debug('ID type'+id.type);
            DateTime dT = id.startdatetime;
            datetime dtref = id.StartDateTime;             
            system.debug('startdatetime'+id.startdatetime);
            myDate = date.newinstance(dT.year(), dT.month(), dT.day());
            //Pull month from startdatetime
            mytime = time.newinstance(dT.hour(), dT.minute(), dT.second(),0);
            Integer WeekOfMonth = Math.ceil((Double)(mydate.Day()) / 7).intValue();               
            system.debug('week of month'+weekofmonth);
            
            //If month = 12 then standard time           
            If (mydate.month()==12){
                system.debug('mydate.month: '+mydate.month());
                id.startdatetime = dt.addHours(-6);          
                system.debug('December DTref:'+ dtref);
                system.debug('December ID after dt.addhours'+id.startdatetime);
            }
            
            // When the month is less than 3 then standard time
            Else if (dt.month() <3){
                id.startdatetime = dt.addhours(-6);
                system.debug('PreMarch'+dt);
                system.debug('PreMarch ID'+id.startdatetime);
            } 
            
            //If date is in March AND before the 2nd weekend then Standard Time
            Else If (dt.month() == 3){
                If(weekofmonth < 3){
                    id.startdatetime = dt.addHours(-6); 
                    system.debug('March'+dt);
                    system.debug('March ID'+id.startdatetime);
                }}
            
            //If date is in November AND after the 1st weekend then Standard Time
            Else if(dt.month()==11){
                If (weekofmonth > 1){
                    id.startdatetime = dt.addHours(-6);
                    system.debug('November'+dt);
                    system.debug('November ID'+id.startdatetime);
                }}
            
            //Everything else is Daylight Savings Time
            Else{
                id.startdatetime = dt.addHours(-5);
                system.debug('DST'+dt);
                system.debug('DST ID'+id.startdatetime);    
            }
        }
    }
}