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
MTBRiderMTBRider 

Issue with Datetime value when using <apex:dynamicComponent>

I am using the CreatedDate column to determine the age of a Case.  Each case is displayed in a pageblock table row.  The pageblocktable is being generateing using apex:dynamicComponent.  The Apex code to generate the column in question looks like this:

....

Component.Apex.column col = new Component.Apex.column();
col.width = '125';
col.headerValue = 'Case Age';

Component.Apex.OutputText ot = new Component.Apex.OutputText();
ot.expressions.value = '{!TEXT(ROUND((NOW() - qc.CreatedDate) * 24, 0))&\'hrs \'&TEXT(ROUND(MOD(ROUND((NOW() - qc.CreatedDate) * 24,3),1)*60,0))&\'mins\'}';

col.childComponents.add(ot);

...

 When I attempt to bring up the VF page, I get this message:

 

Incorrect parameter type for operator '-'. Expected Number, Date, DateTime, received Object

Error is in expression '{!TEXT(ROUND((NOW() - qc.CreatedDate) * 24, 0))&'hrs '&TEXT(ROUND(MOD(ROUND((NOW() - qc.CreatedDate) * 24, 3),1)*60,0))&'mins'}' in component <apex:outputText> in page 
 
However, when I use essentially the same expression in non-dynamically generated visual force:  
 
....

<apex:column headerValue="Case Age">
   <apex:outputText value="{!TEXT(ROUND((NOW() - qc.CreatedDate) * 24, 0))&'hrs'&TEXT(ROUND(MOD(ROUND((NOW() - qc.CreatedDate) * 24, 3),1)*60,0))&'min'}"/>
</apex:column>

...

 ...it works.

 

With the dynamically generated VF, seems as though the datetime is not being treated as a datetime.  I tried wrapping CreatedDate in DATETIMEVALUE() but that did not work either.

 

Anyone have any ideas on this one?

 

Thanks

Vinita_SFDCVinita_SFDC

Hello,

 

TEXT, NOW methods don't work in apex. You need to use String.ValueOf method to convert date to string. Similarly for Now you can use system.now().

 

Please refer followings for list of methods available:

 

http://www.salesforce.com/us/developer/docs/dbcom_apex250/Content/apex_methods_system_string.htm

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_System_Date_static_methods.htm

MTBRiderMTBRider

NOW() is not being used in Apex.   Remember, this is dynamically generated VF.  It is passed as a String to the VF page that is being generated by the Apex code and then evaluated in the VF page.   That is why all those VF functions in the Apex code example are in quotes.  In any case, the function NOW() is not the problem...I am pretty sure that the things is blowing up on the CreatedDate field.

 

GlynAGlynA

I don't know why what you're trying to do doesn't work.  But I have an idea for an alternate implementation.

 

If your iteration variable "qc" was a wrapper class instead of a Case, you could perform the Case Age calculation in your controller using Apex.

 

Something like:

 

public class CaseWrapper
{
    public Case theCase { get; set; }
    public String caseAge
    {
        get
        {
            Long age        = DateTime.now().getTime() - theCase.CreatedDate.getTime();
            Long hours      = age / 3600000;
            Long minutes    = Math.mod( age, hours * 3600000 ) / 60000;

            return String.valueOf( hours ) + 'hrs ' + String.valueOf( minutes ) + 'mins';
        }
    }

    CaseWrapper( Case aCase )
    {
        theCase = aCase;
    }
}

 

The pageBlockTable would iterate over a list of CaseWrapper instances.  All the record fields would be accessed with "qc.theCase.<field>", and the Case Age would be "qc.caseAge".

 

Let me know what you think of this idea.

 

-Glyn