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
Vivekh88Vivekh88 

Insert Datetime using CSV file in VF page

Hi,
I need to insert some records through excel. I have a VF page where i upload the excel file and create records for my custom object. i have a custom field which has Datetime stored. So when i tried to upload the excel with datetime in VF it is going to exception loop. It is not creating records.
This is my class method. 
public void importCSVFile(){
           try{
               csvAsString = csvFileBody.toString();
               csvFileLines = csvAsString.split('\n'); 
               inputvalues = new String[]{};
               for(string st:csvfilelines[0].split(','))
               fieldList.add(st);  
                
               for(Integer i=1;i<csvFileLines.size();i++){
                   AgendaTopics__c accObj = new AgendaTopics__c();
                   string[] csvRecordData = csvFileLines[i].split(',');
                   Datetime dt = Datetime.valueOf(csvRecordData[0]);
                   accObj.Time__c = dt;
                   accObj.Estimated_Duration_minutes__c = Decimal.valueOf(csvRecordData[1]);
                   accObj.Topic_Name__c = csvRecordData[2];    
                   accObj.Owner__c = String.valueOf(csvRecordData[3]);                                                                           
                   acclist.add(accObj);  
               }
}
            catch (Exception e)
            {
                ApexPages.Message errorMessage = new ApexPages.Message(ApexPages.severity.ERROR,'An error has occured while importin data Please make sure input csv file is correct');
                ApexPages.addMessage(errorMessage);
            }

Thanks
Vivek
 
Adrian  GawryszewskiAdrian Gawryszewski
What is the format of the date in you file? Is it passed as a date or a string? valueOf method expects a string as a param. 
Vivekh88Vivekh88
Hi Adrian,
I am passing it as a String.
Adrian  GawryszewskiAdrian Gawryszewski
Ok so the next thing is can you print svRecordData[0] and see if it is passed and check what exact exception your getting. 
Vivekh88Vivekh88
string[] csvRecordData = csvFileLines[i].split(',');
System.debug('csvRecordData++' +csvRecordData);
I tried inserting debug logs for 'csvRecordData' list. 
I am getting the requiered values
11:31:13.0 (34315296)|USER_DEBUG|[72]|DEBUG|csvRecordData++(3/1/2016 6:41, 55, Test 8, Test Contact )
So after this the values are nor assigned to their corresponding fields
accObj.Time__c = Datetime.valueOf(csvRecordData[0]);
accObj.Estimated_Duration_minutes__c = Decimal.valueOf(csvRecordData[1]);
accObj.Topic_Name__c = csvRecordData[2];   
accObj.Owner__c = String.valueOf(csvRecordData[3]);
I am getting exception in there.
 
11:31:13.0 (34923871)|VF_PAGE_MESSAGE|An error has occured while importin data Please make sure input csv file is correct

Thanks
Vivek
 
Adrian  GawryszewskiAdrian Gawryszewski
Cool the issue is the date format you get. To check it you can take that date and try to create a date time using that given 3/1/2016 6:41 datetime in a developer console. If you do it you'll get TypeException: Invalid Type. However if you and PM or AM at the end of that datetime string it should work fine. So 3/1/2016 6:41 PM for instance should not return any exceptions. 

Now you have two options:
1. Better if you ask me is to force the csv file to contain correct datetime format. Don't know how you create them, but for me it sounds like a better idea. 
2. Write a method that will take an input date and format it to correct Salesforce Datetime before it's assign to a field on an object.

Regards
Adrian
Vivekh88Vivekh88
Thanks Adrian for the reply,
for(Integer i=1;i<csvFileLines.size();i++){
                   AgendaTopics__c accObj = new AgendaTopics__c();
                   string[] csvRecordData = csvFileLines[i].split(',');
                   Datetime myDateTime = DateTime.valueof(csvRecordData[0]);
                   String dtConverted = myDateTime.format('yyyy-MM-dd hh:mm:ss');
                   accObj.Time__c = Datetime.valueof(dtConverted);
                   accObj.Estimated_Duration_minutes__c = Decimal.valueOf(csvRecordData[1]);
                   accObj.Topic_Name__c = csvRecordData[2]; 
                   accObj.Owner__c = newreview.id;                                                                         
                   acclist.add(accObj);  
               }
In Developer Console the datetime is saved in 'yyyy-MM-dd hh:mm:ss' format. so i just hardcoded the value in my code and tried to insert.  It was working.
So in this code i tried to convert user input date format to 'yyyy-MM-dd hh:mm:ss' but still i am getting the same error.
The method what i gave for converting the datetime format is correct or do i need to change something?

Thanks
Vivek
Adrian  GawryszewskiAdrian Gawryszewski
Well the idea is good however the issue still lies in line 4. You won't be able to create a new DateTime from unapropriate string format. So it doesn't matter that you try to change the format later on and assign it to the field. Try to change it before line 4. So create a string method that adds that additional postfix PM or AM after the date and test it.