4

I'm passing a date/time field as a parameter to a page like this:

&startDate={!object.Start_date__c}

Start_date__c in object is a Date/Time field.

But when I open the link I get this error:

System.TypeException: Invalid date/time 

In the page I'm linking with that parameter, which I can not modify for now, I can see that this is the line that generates the error:

DateTime startDate  =   DateTime.valueOf(startDateInputString);

How can I modify the parameter in input to make this work?

The problem seems to be that I'm passing 01-24-2013 1.00 with a blank space in my url. Thanks

1
  • maybe related to the date getting url encoded. Try DateTime.valueOf(EncodingUtil.urlDecode(startDateInputString))
    – Phil Rymek
    Commented Jan 24, 2014 at 15:58

3 Answers 3

4

Here's what a DateTime looks like when blatted out on a page as a string:

(format one)

Fri Nov 30 21:55:38 GMT 2012

You can't just hydrate that string back into a DateTime. You must construct or deserialize it.

Here's what a serialized DateTime looks like:

(format two)

"2014-01-24T16:56:27.044Z"

It's not the same as the above string output. You need something more like this:

public DateTime dateFromUrl() {
    String serializedDate = ApexPages.currentPage().getParameters().get('startDate');
    return (DateTime)Json.deserialize(serializedDate, DateTime.class);
}

And as a good web citizen, if you're serving up the parameter in a link it should be URL encoded:

https://c.na1.visual.force.com/apex/MyPage?startDate=2014-01-24T16%3A56%3A27.044Z

And you might note that the DateTime.valueOf method expects yet a different value:

(format three)

yyyy-MM-dd HH:mm:ss

Knowing these, we can ensure that we are creating (and expecting) matching date formats.

2

I would try encoding the datetime to get a properly encoded url param, then decoding in your controller...

Before adding to the url...

&startDate={!URLENCODE(object.Start_date__c)}

In your controller on the receiving end...

DateTime dt = DateTime.valueOf(EncodingUtil.urlDecode(startDateInputString));
2
  • Thanks! {!URLENCODE(object.Start_date__c)} doesnt work and gives error Incorrect argument type for function 'URLENCODE(), seems it accepts string only and datetime/date when provided throws error. Commented May 28, 2015 at 15:10
  • Also the urlDecode needs 2nd parameter too of Encoding Type for eg startOfWeek = Date.valueOf(EncodingUtil.urlDecode(startOfWeekParam,'UTF-8')); Commented May 28, 2015 at 15:11
0

Set this in the URL:

?startDate={!URLENCODE(TEXT(object.Start_date__c))}

and in the controller try this code:

Date startDate =  Date.valueOf( EncodingUtil.urlDecode( ApexPages.currentPage().getParameters().get('startDate'), 'UTF-8') );

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .