4

I am trying to upload the CSV file via Apex programming I am getting an error like Invalid Date Error. Please find the screenshot for more details. I have given code like this

site.Approved_Date__c =date.parse(inputsitevalues[13]);//date field [12].

Even though I have changed the date format as dd/mm/yyyy in CSV file I am getting the same error. Can anyone help me out in this issue.

enter image description here

7

It looks like the CSV file has the dates in mm/dd/yyyy format rather than dd/mm/yyyy. Hence the invalid date exception with the 31st month.

This will work:

date parsedDate = date.parse('31/5/2013');

This will fail with "System.TypeException: Invalid date: 5/31/2013"

date parsedDate = date.parse('5/31/2013');

From the Date.parse methods docs:

Constructs a Date from a String. The format of the String depends on the local date format. The following example works in some locales:
date mydate = date.parse('12/27/2009');

You can find the locale Date format information in the Supported Locales docs.

Another option would be to use the date format YYYY-mm-dd and date.valueOf().

  • Thanks for some useful information. My CSV file date format is DD/MM/YYYY and my organization date format also the same. My organization default locale English (Australia). When I am trying to upload the CSV file which is generated by Google drive is working fine as per my requirement but other local UTF-8 CSV files are giving the Invalid Date error. Can u pls give me some permanent solution for this. – Arun Jul 22 '13 at 10:31
  • Go to your CSV files and examine them in Notepad - look at how the dates are saved in the files. Your APEX code will have to be written to be sufficiently flexible to handle all variations. There is no one single APEX Date method that will parse all formats. – cropredy Jul 22 '13 at 16:12
  • @Arun, It would appear that your local UTF-8 CSV files are not in the format you expected, as 5/31/2014 would not be a valid date in the en_AU locale. I don't think you can have generic code to handle this. Even if you caught the TypeException and tried an alternative parsing method, you need to know the date format used to be consistent with all the data. – Daniel Ballinger Jul 22 '13 at 19:27
  • Excel might display the date as dd/mm/yyyy but that is a bit like SF displaying dates and times in the locale of the user. I overcame this by constructing a textstring using an excel formula and then used that. – ITMANHAH Jul 23 '13 at 5:34
  • Thank you all for the valuable inputs. From all your inputs I came to know that this will be a file format issue so we cannot have any generic code to resolve this error. – Arun Jul 23 '13 at 6:54
1

One thing you can do -- Different dataloaders can pre-parse dates and manipulate them into a better format. Specifically, I believe Jitterbit's dataloader has the ability to parse multiple date formats and reset them into a Salesforce friendly way. Try looking at some of the Functions in jitterbit to see if can re-write the date on the fly.

1

You can use split option to parse date from csv file like below :

string[] RFQdate = String.valueOf(csvRecordData[1].trim()).split('/'); date mydate = date.valueOf(RFQdate[2] + '-' + RFQdate[0] + '-' + RFQdate[1]);

default parsing of date fields depend on settings of your locales in your org. Use this link to check your settings : https://help.salesforce.com/apex/HTViewHelpDoc?id=admin_supported_locales.htm&language=en

0

Thank you all for the answers. This is purely a format issue. we have interchanged the columns and keep date values far away from the end. Here, we’ve moved the columns in such a way that there are 5 more columns after dates. Now it is working perfectly with out any Invalid date issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.