+ Start a Discussion

Need Help - Converting Date/Time fields to Date and String

Hi Everyone,


I have a trigger that kicks off when a user drags and drops an event (Call) from one timeslot to another. This trigger updates the Date, Start Time, and End Time fields on the Call object to whatever the new timeslot is on the Calendar. However, the ActivityDate, StartDateTime, and EndDateTime on the "Event" object are all Date/Time fields. I want these fields to map to the appropriate fields on the "Call" object, but the field types are different:


  • Call_Date__c: Date
  • Start_Time__c: Picklist
  • End_Time_c: Picklist

Unforutnately, I don't want to change the field types on the Call object, since I'd lose all data associated to those fields. Basically, I want the following code to work, but I need to know where and how to convert the Datetime fields on the "Event" object to strings (or whatever they need to be):


Callid  = evt.Whatid ;                 
                            cnt = [SELECT count() FROM Call__c WHERE id =: Callid  ];
                            if(cnt == 1){
                                UpdateCall = [SELECT Call_Date__c, Start_Time__c, End_Time__c FROM Call__c 
                                    WHERE id =: Callid   ];
                                UpdateCall.Call_Date__c = evt.ActivityDate;
                                UpdateCall.Start_Time__c = evt.StartDateTime;
                                UpdateCall.End_Time__c = evt.EndDateTime;  
                                System.debug('createCallEvent: the StartDateTime ' + 
                                System.debug('createCallEvent: the StartDateTime from New Map' +
                                   System.Trigger.newMap.get(evt.Id).StartDateTime + 'for id '+ evt.Id );                        
                                System.debug('createCallEvent: the StartDateTime from Old Map' +
                                   System.Trigger.oldMap.get(evt.Id).StartDateTime + 'for id '+ evt.Id );                        
                                UpdateCall.EventUpdate__c = true;
                                update UpdateCall; 


The picklist values for the Start_Time__c and End_Time_c fields are "08:00 AM" to "08:00 PM", with 30 minute increments. The error that I currently get when I try to compile my trigger is


Error: Compile Error: Illegal assignment from Datetime to String at line 50 column 33.


Any help will be greatly appreicated!


Thank you!



Best Answer chosen by Admin (Salesforce Developers) 

You should be able to use the date/time methods for this.



UpdateCall.Call_Date__c = evt.ActivityDate.date();
UpdateCall.Start_Time__c = evt.StartDateTime.format('KK:mm a');
UpdateCall.End_Time__c = evt.EndDateTime.format('KK:mm a');