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
Aiden ByrneAiden Byrne 

<apex:outputfield> like formatting in APEX

I'm passing back some field values via SOAP to a mobile client. 

 

I'd like to pass these back as formatted text to make life easier for the mobile developer. So, a currency field would be correctly formatted as currency, a number field with commas etc. 

 

In VisualForce, I can get great default formatting via the apex:outputfield. If I try to do the conversion to a string in APEX via string.valueOf, I get a pretty crude string (e.g. for numbers I get an exponential representation). 

 

Is there any way I can access the VisualForce like formatting used in Apex:OutputField from within APEX?

 

thanks!

Aiden

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Unfortunately the mechanism that should lend itself to this - String.format(), doesn't work in this way.  There's a blog post exploring this at:

 

http://www.tgerm.com/2011/01/message-format-string-format-apex.html

 

 

All Answers

gm_sfdc_powerdegm_sfdc_powerde

Not that I am aware of.  I see where you are going with this.  But I doubt if it's a good idea to get the formatted text produced by outputField since it might include some custom JavaScript and references to styles that may not work if you blindly embed the snippet on the mobile UI.

Aiden ByrneAiden Byrne

Agreed, I mentioned outputField as there is code somewhere in Salesforce.COM that does great default formatting for an sObject field to text that then may have additional javascript attribution applied to it. So, I'm not trying to get access to OutputField from within APEX, rather to the same logic that lets OutputField build a nicely formatted string. 

bob_buzzardbob_buzzard

Unfortunately the mechanism that should lend itself to this - String.format(), doesn't work in this way.  There's a blog post exploring this at:

 

http://www.tgerm.com/2011/01/message-format-string-format-apex.html

 

 

This was selected as the best answer
Aiden ByrneAiden Byrne

Ok, thanks all