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
sfdc dev 2264sfdc dev 2264 

copying value from a text field to a date field

Hi,

I need help on the below requirement as follows,

I have a TEXT field on case object which stores the value in DD:MM:YYYY HH:MM:SS (11/12/2018 11:22:23) format

I have another DATE field which i need to copy the date value alone from the previous text field and store it in date field.

Please let me know how to achieve it 

Thanks
Best Answer chosen by sfdc dev 2264
sfdc dev 2264sfdc dev 2264
Hi,

I tried the below code
public class QCC_casedeparturedatecalculation {
    
    public static void datecalculation(List<Case> caseList )
    {
        if(caseList.size() >0)
        {
            List<Case> caseList2  = new List<Case>();
            for(Case c: caseList)
            {
                //Case ca = new Case();
                String firstDate = c.Departure_Date_Time__c;
              
                String month = firstDate.substring(0,2);
                String day = firstDate.substring(3,5);
                String year = firstDate.substring(6,10);
          
                string hour = firstDate.substring(11,13);
                
                string minute = firstDate.substring(14,16);
                
                string second = firstDate.substring(17,19);
                  
                string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' 
                    + minute +  ':' + second;
                System.debug('stringDate:'+stringDate);
                
                Date myDate = Date.valueOf(stringDate);
                c.Updated_Departure_Date_Time__c = myDate;
            
            }
        }
        
    }
    
}

The date is populating but the values are coming but wrong values

for eg:

the original field one had 

15/03/2018 13:30:00

the new field value came as

3/03/2019


 

All Answers

Akshay_DhimanAkshay_Dhiman
Hi,

In Apex you could use the following code write trigger:
 
Integer year = Integer.valueOf(FieldApiName.subString(6,10));
Integer month = Integer.valueOf(FieldApiName.subString(3,5));
Integer day = Integer.valueOf(FieldApiName.subString(0,2))
Date d = Date.newInstance(year, month,day);  
System.debug(str.subString(0,2)+'/'+str.subString(3,5)+'/'+str.subString(6,10));

If you're doing this in a Field Update the following formula should work.
or 
 Creates a formula field which return type is date and use this formula.

DATE(VALUE(MID(Date__c, 4,4)), VALUE(MID(Date__c,2,2)), VALUE(LEFT(Date__c,2)))

if you found this answer helpful then please mark it as best answer so it can help others.      
  
  Thanks 
  Akshay
sfdc dev 2264sfdc dev 2264
I tried the above formula through workflow field update

DATE(VALUE(MID(Departure_Date_Time__c, 4,4)), VALUE(MID(Departure_Date_Time__c,2,2)), VALUE(LEFT(Departure_Date_Time__c,2)))


but when try to save the record it am getting the below error

Action Field Update Error
A workflow or approval field update caused an error when saving this record. Contact your administrator to resolve it. Updated Departure Date/Time: value not of required type: sfdc.formula.impl.InvalidNumericValueException: The value &#39;03/2&#39; is not a valid number. 



15/03/2018 13:30:00

Please help me
Akshay_DhimanAkshay_Dhiman
Hi sfdc dev 2264
 
//Trigger
trigger Case_Trigger on Case (before insert,before update) {
     ABCTESTING.find(Trigger.new);
}
Try This Code:
//Helper class:
public class ABCTESTING {
    
    public static void find(List<Case> caseList )
    {
        if(caseList.size() >0)
        {
            List<Case> caseList2  = new List<Case>();
            for(Case c: caseList)
            {
                //Case ca = new Case();
                String firstDate = c.Date1__c;
              
                String month = firstDate.substring(0,2);
                String day = firstDate.substring(3,5);
                String year = firstDate.substring(6,10);
          
                string hour = firstDate.substring(11,13);
                
                string minute = firstDate.substring(14,16);
                
                string second = firstDate.substring(17,19);
                  
                string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' 
                    + minute +  ':' + second;
                System.debug('stringDate:'+stringDate);
                
                Datetime myDate = Datetime.valueOf(stringDate);
                c.Date2__c = myDate;
            
            }
        }
        
    }
    
}


Thanks
Akshay
sfdc dev 2264sfdc dev 2264
Hi,

I tried the below code
public class QCC_casedeparturedatecalculation {
    
    public static void datecalculation(List<Case> caseList )
    {
        if(caseList.size() >0)
        {
            List<Case> caseList2  = new List<Case>();
            for(Case c: caseList)
            {
                //Case ca = new Case();
                String firstDate = c.Departure_Date_Time__c;
              
                String month = firstDate.substring(0,2);
                String day = firstDate.substring(3,5);
                String year = firstDate.substring(6,10);
          
                string hour = firstDate.substring(11,13);
                
                string minute = firstDate.substring(14,16);
                
                string second = firstDate.substring(17,19);
                  
                string stringDate = year + '-' + month + '-' + day + ' ' + hour + ':' 
                    + minute +  ':' + second;
                System.debug('stringDate:'+stringDate);
                
                Date myDate = Date.valueOf(stringDate);
                c.Updated_Departure_Date_Time__c = myDate;
            
            }
        }
        
    }
    
}

The date is populating but the values are coming but wrong values

for eg:

the original field one had 

15/03/2018 13:30:00

the new field value came as

3/03/2019


 
This was selected as the best answer
sfdc dev 2264sfdc dev 2264
i got the issue bro

the values for day and month was mapped wrongly , i changed its working now , thanks for your help