I have an extremely simple requirement. There is a table which shows files and each files size in bytes:

<apex:column value="{!file.BodyLength} bytes" />

I simply want to display the length in KB instead of bytes. I imagined VisualForce would allow something like the following:

<apex:column value="{!file.BodyLength / 1024} KB" />

But it doesn't. Is a formula field the only way to go about this? There is no simple way to do this directly in VisualForce?

  • 2
    I just created a gist that shows it does work. What problem are you having? – sfdcfox Apr 7 '16 at 17:23
  • If I try anything like <apex:column value="{!ml.Mileage__c / 100}" /> on a numeric field, I get the following error: "Invalid field Mileage__c / 100 for SObject ... " I'm not trying on the BodyLength field for now, but Mileage__c is a numeric field on another sObject and it should work here ... – Mehdi Maujood Apr 7 '16 at 17:41
  • As per sfdcfox, try <apex:column value="{!floor(file.BodyLength / 1024)} KB" /> – Anurag Apr 7 '16 at 17:52
  • 2
    @MehdiMaujood I was able to replicate the error with standard fields. I created a gist that explains the problem and workaround. – sfdcfox Apr 7 '16 at 18:05

It appears that the "value" attribute on apex:column has a bug in it. For now, you can expand your apex:column code to render the value within the apex:column's content:

    {!ml.Milage__c / 100}

I'm going to send an ask to salesforce.com and see if I find out why this is broken (and if it's going to be fixed).

| improve this answer | |
  • I'm not surprised at this -- value= for some VF components has to resolve to a controller variable and expressions would be problematic; e.g. <apex:outputField value="{!someInlineExpression}"/> – cropredy Apr 8 '16 at 1:35

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.