Is there a way to call an Apex method with input parameters from Visual Force?

For example, if I have this Apex method:

public static String formatAddress(String city, String state, String country){
    String formatted = city;
        formatted += ", " + city;

    if(country != "US"){
        formatted += ", " + country;

    return formatted;

And I have this VF page:

<apex:repeat value="{!fooList}" var="foo">
    <apex:outputText value="{!formatAddress(foo.city, foo.state, foo.country)}" />

I know I can put the city/state/country values up as page params, and pull them in Apex from the page params map, or put them in member variables on the controller, but I was looking for a way to do this without having to do any temporary storing of the data.


edit - sorry I couldn't reply to the comments below, but to provide more clarification:

@Rao - I've never used js remoting before, but quick glance at your link it looked like it will do ajax calls and update the dom later on. I don't think this will work in my situation since the info I'm trying to print is not dynamic and will not need to be updated via ajax. also, since I'm looping through a list, I don't think it would be a good idea to do ajax calls within a loop

@IllusiveBrian - I can definitely store the formatted addresses in a map, but I'm trying to keep my view state low. this is definitely a viable solution though

@sfdcfox - I was going to do this but I thought I'd end up storing the data in memory twice, one for the actual "foo", and another for the "fooWrapper", which isn't bad but I was just looking for a leaner solution

  • 1
    Are you open to an approach using javascript remoting?developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… – Rao Feb 24 '17 at 17:31
  • If the requirement is for the Visualforce renderer to call the function with parameters, javascript remoting and actionFunction are not really meeting that requirement. FWIW, I usually implement requirements like this with a Map<foo, String> or Map<ID, String> that I assign to before passing control to the renderer. – IllusiveBrian Feb 24 '17 at 19:37

No, you can't use parameters in the context you're using. Generally, I'd get around this by writing a wrapper class:

public class Wrapper {
  public Wrapper(Account record) {
    rec = record;
  account rec;
  public String getFormattedAddress() {
    // Do your formatting here
| improve this answer | |
  • Would this be called as apex:outputText value="{!foo.formattedAddress}" ? – IllusiveBrian Feb 24 '17 at 19:58
  • @IllusiveBrian Yes, that's how you'd call it. – sfdcfox Feb 24 '17 at 19:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.