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
Cris9931Cris9931 

display the maximum and minimum datetime from a text field

Hi, I have a master-detail relationship between two custom objects. Work Order(master) - Work Detail(detail).

On Work Detail I have 2 fields which displays Date/Time but in a text field format. Like this:

User-added image

 

How to make a trigger without time conversion to display the maximum and minimum dateTime from the child(work detail)?

I started writing the trigger:

List<Id> listWoId = new List<Id>();
        
        for(SVMXC__Service_Order__c loopwo : Trigger.new){
              listWoId.add(loopwo.Id);
        }
        
        List<SVMXC__Service_Order_Line__c> currentWL = [SELECT ID, Name, SIG_Start_Date_Time_Device__c , SIG_End_Date_Time_Device__c from SVMXC__Service_Order_Line__c where SVMXC__Service_Order__c =: listWoId]; 
        
        for(SVMXC__Service_Order_Line__c workDetail : currentWL ){

          DateTime dt = DateTime.valueOf(workDetail.SIG_Start_Date_Time_Device__c);
          System.debug('dt' + dt);
        }

But this is not working, I tried to change the timezone from my user and everytime I change and System.Debug I see different DateTime because of the timezone conversion...how can I avoid this?
AnudeepAnudeep (Salesforce Developers) 
You can leverage other methods in the DateTime class
 
Map <String, Integer> monthNames = new Map <String, Integer> {'Jan'=>1, 'Feb'=>2, 'Mar'=>3, 'Apr'=>4, 'May'=>5, 'Jun'=>6, 'Jul'=>7, 'Aug'=>8, 'Sep'=>9, 'Oct'=>10, 'Nov'=>11, 'Dec'=>12};
List <String> stringParts = 'Wed Aug 07 04:30:00 GMT 2013'.split(' ');
List <String> timeParts = stringParts[3].split(':');

DateTime yourDateVariable = DateTime.newInstanceGmt(Integer.valueOf(stringParts[5]), monthNames.get(stringParts[1]), Integer.valueOf(stringParts[2]), Integer.valueOf(timeParts[0]), Integer.valueOf(timeParts[1]), Integer.valueOf(timeParts[2]));

let me know if it helps