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
Jega DeivaJega Deiva 

Date field in trigger shows wrong time in Email

Hi all,

This is my trigger from trailhead  "Conference Management App".
In that "Session Date" datatype="Date" it shows incorrect time also (Screenshot attached).Please help me in this.

trigger SendConfirmationEmail on Session_Speaker__c (after insert) {

    Set<ID> ids = new Set<ID>();
 for(Session_Speaker__c newItem : trigger.new) {

        ids.add(newItem.Id);

    }
// Retrieve session name and time + speaker name and email address

    List<Session_Speaker__c> sessionSpeakers =

            [SELECT Session__r.Name,

                    Session__r.Session_Date__c,
                    Session__r.Session_Time__c,

                  Speaker__r.Name,

                   Speaker__r.Email_Id__c

             FROM Session_Speaker__c

             WHERE Id IN :ids];
 if(sessionSpeakers.size() > 0) {

        List<EmailManager.newEmail> emailList = new List<EmailManager.newEmail>();

         for(Session_Speaker__c ss : sessionSpeakers) {

            // Send confirmation email if we know the speaker's email address

            if (ss.Speaker__r.Email_Id__c != null) {

                String[] addresses = new String[]{ss.Speaker__r.Email_Id__c};

                String subject = 'Speaker Confirmation';

                String message = 'Dear ' + ss.Speaker__r.Name +
                
                ',\n\nThank you for agreeing to speak at the '+  ss.Session__r.Name + 'I am sure your presence will enhance the experience of all who to attend.\n\nI am sending this email to provide some basic information about the conference and our expectations of you.\n\n'

+'You are scheduled to speak on\n' + ss.Session__r.Session_Date__c + 'at\n' + ss.Session__r.Session_Time__c + ',\n\n Again thank you for helping us with this event !.';
 EmailManager.newEmail email = new EmailManager.newEmail(addresses, subject, message);

                emailList.add(email);

            }

        }

          if(!emailList.isEmpty()) {

            EmailManager.sendMail(emailList);
  }

    }
}

Thanks
Jega
User-added image
 
AnudeepAnudeep (Salesforce Developers) 
Hi Jega, 

The screenshot shows time as 2020-03-20 00:00:00. What is the correct value that you are expecting to be displayed in the email?

All Date/Time fields are stored in UTC (Universal Time Coordinated) time. If your user's time zone is not GMT, but for example GMT+1, an extra hour will be added to the time in Salesforce

If you run the below in the dev console you will see that the time is adjusted accordingly to your time zone.

For example: 

DateTime time1 = DateTime.parse('01/01/2013 03:00');
DateTime time2 = DateTime.parse('01/06/2013 03:00');
System.debug(time1);
System.debug(time2);

14:28:16:002 USER_DEBUG [3]|DEBUG|2013-01-01 03:00:00
14:28:16:002 USER_DEBUG [4]|DEBUG|2013-06-01 02:00:00

You can add system debugs and use developer console to troubleshoot this

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thank you!
Jega DeivaJega Deiva
Hi Anudeep,

 Thanks for your reply.
 One more doubt, "Session Date" is a Date field, I don't want to display the time  2020-03-20 00:00:00 in that.
 Is it possible to show only Date value "2020-03-20 " ?

Jega
AnudeepAnudeep (Salesforce Developers) 
Hi Jega, 

It is possible to show only date value. You can achieve this using format() method

date myDate = date.newInstance(2001, 3, 21);
String dayString = myDate.format();
System.debug('format' + dayString);

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_date.htm

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. Thank you

Anudeep