1
CalEvent calEvent = (CalEvent) JSON.deserialize(calEventJson,  Technician_Dispatch_Util.CalEvent.class);
System.debug('OGDEBUG TDU calEventJson '+calEventJson);
System.debug('OGDEBUG TDU updateTechnicianCalEvent calEvent.startTime '+ calEvent.startTime + ' calEvent.endTime '+calEvent.endTime);

15:13:56.110 (110381617)|USER_DEBUG|[185]|DEBUG|OGDEBUG TDU calEventJson {"id":zzzzzzzzzzz","title":"zzzzz New College of zzz","allDay":false,"startTime":"2015-07-27T00:00:00.000","endTime":"2015-07-27T01:00:00.000","ownerId":"zzzzzzzzzz","description":"Perform PM visit as detailed on supplied spreadsheet. Check service office's documentation for equipment list. Contact dispatcher at Help Desk with any questions. For damaged equipment please specify the following:\n\n* Room Name:\n* Manufacturer:\n* Model:\n* Serial No.:\n* Problem: \n\nROOMS TO COVER:\n\nNew College of zz- Sarasota Fl - Carriage House\n\nNew College of zz- Sarasota zz- CHL 0\n\nNew College of zz- Fl - CHL 2z1"}

15:13:56.110 (110791126)|USER_DEBUG|[186]|DEBUG|OGDEBUG TDU updateTechnicianCalEvent calEvent.startTime 2015-07-27 04:00:00 calEvent.endTime 2015-07-27 05:00:00

You can see from the above log output that the json being passed in are correct:

"startTime":"2015-07-27T00:00:00.000"

"endTime":"2015-07-27T01:00:00.000"

However after calling JSON.deserialize time is added onto them (4 hours)

calEvent.startTime 2015-07-27 04:00:00

calEvent.endTime 2015-07-27 05:00:00

Why is this and how can I prevent against it?

FYI Troubleshooting further I tried something different

Map<String, Object> resultsMap = (Map<String, Object>) JSON.deserializeUntyped(calEventJson);
    String startTime = (String)resultsMap.get('startTime');//hack for issue with incorrect time saved
    String endTime = (String)resultsMap.get('endTime');

This startTime will have the correct String time But I cant convert it to Datetime as I get errors

Invalid date/time: 2015-07-28T00:00:00.000


2 Answers 2

2

DateTime supports the presentation of the Date/Time in both GMT and in the time zone of the User. Is the User you are testing under in a time zone that is 4 hours different from GMT? Your debug output is I think being presented in the timezone of your User rather than in the GMT timezone.

If you change your debug output to use calEvent.startTime.timeGmt() and calEvent.endTime.timeGmt() I think you will see that the values from the JSON are preserved.

5
  • Thank you very much that does resolve the discrepancy. However I cannot assign this to my DateTime object as it strips out the date. How can I get my DateTime object to have my date and time as GMT
    – owen gerig
    Commented Aug 19, 2015 at 21:31
  • @owengerig The values are held in a DateTime as GMT and offset to the User's timezone in some of the methods and not in others. For example the getTime method is GMT based. I don't understand what you mean by "I cannot assign this to my DateTime object".
    – Keith C
    Commented Aug 19, 2015 at 21:36
  • So whether im saving them or retrieving them they are DateTime objects. But its only when i do .timeGmt() that the hours are correct. if i just read or save the StartDateTime object it reads the +4 hour time. Basically I need DateTime object with StartDateTime's date and StartDateTime's time get what is returned from timeGmt()
    – owen gerig
    Commented Aug 19, 2015 at 21:40
  • @owengerig If you really want to deal with dates not date/times then grab the GMT date using dateGmt and just work with the dates. If you use DateTime across your code, and want to stay in GMT, you will have to be careful to use the various methods ending in "Gmt" everywhere.
    – Keith C
    Commented Aug 19, 2015 at 21:44
  • no i want both date and time but i need it without the users timezone coming into play. thank you though
    – owen gerig
    Commented Aug 19, 2015 at 21:50
-1

The below code solved our issue, After deserialisation the time was getting changed, inorder to have the exact time make use of Time.newInstance and pass Hours, minutes and seconds, that will give us the exact time without changing, refer the below logic to implement the same.For example here the input is 12:30 AM then it will be inserted as 12:30 AM

public class businessHourInsert {
   @AuraEnabled
    public static void insertBH(String strBhRecord){        
        try{
            System.debug('Inside the Apex  8 -->'+strBhRecord );
            
            Map<String, object> m =   (Map<String, object>) JSON.deserializeUntyped(strBhRecord);
            
            System.debug('BusinessHours>>>>' + m);            
            
            BusinessHours b = new BusinessHours();
            b.Name = (string)m.get('BhName');
            b.SundayStartTime = returnTime((string)(m.get('SundayStart')));
            b.IsActive = true;
            b.IsDefault= false;
            b.TimeZoneSidKey=(string) m.get('TimeZoneKey');            
            b.SundayEndTime = returnTime((string)(m.get('SundayEnd')));            
            System.debug('b---'+b);
            //  System.debug('result.SundayStart--result.SundayEnd-'+result.SundayEnd+'  '+result.SundayStart);            
            Map<String, Object> params = new Map<String, Object>();            
            params.put('InputBusinessHours', b);
            Flow.Interview.Create_Update_BH BhConfigFlow = new Flow.Interview.Create_Update_BH(params);
            BhConfigFlow.start();
  
        }catch(Exception ex){
            throw new AuraHandledException(ex.getMessage());
        }
    }
    
    public static time returnTime(string strTimeVar){
        String timeStr = (string)strTimeVar;
        String defaultTime = '00:00:00';          
        String[] ct = (timeStr != null) ? timeStr.split(':') :defaultTime.split(':');          
        Time t = Time.newInstance(Integer.valueOf(ct[0]), Integer.valueOf(ct[1]), Integer.valueOf(ct[1]), 0);
        return t;        
    }
    
    public class Bhrs {
        public String BhName;
        public Time SundayStart;
        public Time SundayEnd;
        public String TimeZoneKey;
        
    }
}
2
  • I've downvoted this because it's a code dump. The explanation is as important as the solution. You should edit your answer to include details about what the problem is and how this fixes it.
    – Derek F
    Commented Nov 18, 2022 at 15:23
  • I really don't care about your down vote Commented Nov 19, 2022 at 3:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .