0

In my code I try to set datetime based on selected location. The Idea is, when the user select locale and then the time, record will be saved with selected datetime value based on selected locale. But this is not working. I'm also getting datetime in my current locale and not in updated.

My apex code is below:

public class  NewTaskSchedulerController {

public String StartDate {get; set;}

public void ChangeTimeZone() {   
    User us = CurrentUser;
    if (event.Activity_Location__c == 'Geneva'){us.TimeZoneSidKey = 'Europe/Rome';}
    Database.update(us); 
}
public void ChangeTimeZoneBack() {
    User usupdate = CurrentUser;  
    usupdate.TimeZoneSidKey = string.valueOf(UserTimeZone);
    update usupdate;     
}
public Datetime parse(String startDate, String startHour) {
    List<String> hourComponents = startHour.split(':');
    return Datetime.newInstance(Date.parse(startDate),Time.newInstance(Integer.valueOf(hourComponents[0]),Integer.valueOf(hourComponents[1]),0, 0));        
}
public PageReference save() {        
    try{
        //My Current location is Europe/Moscow GMT +03:00
        //event.Activity_Location__c == 'Geneva' adn GMT is +02:00
        //StartDate = '01/06/2019 17:00'
        ChangeTimeZone();
        String[] str = StartDate.split(' ');                                         
        DateTime dt = parse(str[0], str[1]);
        system.debug('dt='+dt);
        //After execution I get dt = '01/06/2019 14:00'

        Event ev = new Event();
        event.ActivityDateTime = dt; 
        insert event;
        ChangeTimeZoneBack();
    }
    catch (Exception exc) {
        ChangeTimeZoneBack();
    }
}
}

How can I get expected result ('01/06/2019 15:00')?

  • Just out of the curiosity -- is given code working? No Mixed DML exception due to developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…? – kurunve May 31 at 11:37
  • @kurunve Yes It works fine, after ChangeTimeZone() I get current TimeZoneSidKey = 'Europe/Rome'. But in some reason the parse, format, newInstance methods thinks that my TimeZone is 'Europe/Moscow' – Viktor May 31 at 12:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.