Search code examples
apexvisualforce

Apex DateTime to String then pass the argument


I have two Visualforce pages, and for several reasons I've decided it is best to pass a datetime value between them as a string. However, after my implementation my date always appear to be null even though my code seems to compile.

I have researched quite a few formatting topics but unfortunately each variation of the format() on date time seems to not produce different results.

The controller on page 1 declares two public variables

public datetime qcdate;
public String fdt;

qcdate is generated from a SOQL query.

 wo = [SELECT id, WorkOrderNumber, Quality_Control_Timestamp__c FROM WorkOrder WHERE id=:woid];
 qcdate = wo.Quality_Control_Timestamp__c;

fdt is then generated from a method

fdt = getMyFormattedDate(qcdate);

which looks like this

public String getMyFormattedDate(datetime dt){
    return dt.format();    }

fdt is then passed in the URL to my next VF page.

String url = '/apex/workordermaintenanceinvoice?tenlan='+tenlan +'&woid=' + woid + '&invnum=' + invnum + '&addr1=' + addr1 + 'fdt=' + fdt;
PageReference pr = new PageReference(url);

I expected when calling {!fdt} on my next page to get a proper string. But I do not.

UPDATE:

Sorry I guess I should not have assumed that it was taken for granted that the passed variable was called correctly. Once the variable is passed the following happens:

The new page controller creates the variable:

public String fdt

The variable is captured with a getparameters().

fdt = apexpages.currentPage().getparameters().get('fdt');

The getfdt() method is created

public String getfdt(){
    return fdt;
}

Which is then called on the VF page

{!fdt}

This all of course still yields a 'blank' date which is the mystery I'm still trying to solve.


Solution

  • You passed it via URL, cool. But... so what? Page params don't get magically parsed into class variables on the target page. Like if you have a record-specific page and you know in the url there's /apex/SomePage?id=001.... - that doesn't automatically mean your VF page will have anything in {!id} or that your apex controller (if there's any) will have id class variable. The only "magic" thing you get in apex is the standard controller and hopefully it'll have something in sc.getId() but that's it.

    In fact it'd be very stupid and dangerous. Imagine code like Integer integer = 5, that'd be fun to debug, in next line you type "integer" and what would that be, the type or variable? Having a variable named "id" would be bad idea too.

    If you want to access the fdt URL param in target page in pure Visualforce, something like {!$CurrentPage.parameters.fdt} should work OK. If you need it in Apex - you'll have to parse it out of the URL. Similar thing, ApexPages.currentPage() and then call getParameters() on it.

    (Similarly it's cleaner to set parameters that way too, not hand-crafting the URL and "&" signs manually. If you do it manual you theoretically should escape special characters... Let apex do it for you.