2

This question already has an answer here:

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;
    if(!String.isEmpty(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)}" />
</apex:repeat>

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.

Thanks


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

marked as duplicate by Eric, Santanu Boral, Tushar Sharma, Ratan Paul, Boris Bachovski Feb 27 '17 at 1:01

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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
3

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
  }
}
  • 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.