I'm struggling with syntax here.

This is what I'm trying to do - obviously the syntax is wrong.

<apex:actionsupport event="onchange" action="{!IF(x=1, {!save}, {!otherMethod})}"

How would I format the {!action}'s in the above line for it to work?

  • I think you need to use Javascript to determine which action method to be invoked. Do you need a example for that? – javanoob Mar 11 '16 at 0:25
  • Hm. The javascript route would work quite well. Can you call javascript from an "Action="? I thought you can only call JS from "onchange/onclick/ondblclick" etc. – RC82 Mar 11 '16 at 0:55
  • You are correct. We cannot call javascript from action property. – javanoob Mar 11 '16 at 1:03
  • That was my issue. I already had JS triggered on the "oncomplete", but I wanted an action ran before that depending on some criteria... which I just realized I could have done that in the JS anyways, and just used an empty dummy method in the "action="" for the apex actionsupport. Thanks for feedback, helped me realize what I should have done! – RC82 Mar 11 '16 at 1:52
  • I think the another option as mentioned by Boris in the comments is to have a delegated action method on the controller which does the conditional check before calling the correct method. – javanoob Mar 11 '16 at 1:55

{! } is the markup for the expression, once you're in, there is no need to add another expression, everything inside is going to be evaluated. Try {!IF(x = 1, save(), otherMethod())}. I haven't tried this with the action parameter and methods, but in general that's how it works.

Alternatively, you can have 2 separate <apex:actionsupport /> tags and render them conditionally:

<apex:outputPanel layout="none" rendered="{!x = 1}">
    <apex:actionsupport event="onchange" action="{!save}"/>
<apex:outputPanel layout="none" rendered="{!x != 1}">
    <apex:actionsupport event="onchange" action="{!otherMethod}"/>
| improve this answer | |
  • Hey Boris, thanks for the quick Reply! I'm getting a "Unknown function save. check spelling" error. It definitely IS called save - "public void save()" - thoughts? – RC82 Mar 11 '16 at 0:16
  • Maybe try just 'save' without the brackets in VF? – Boris Bachovski Mar 11 '16 at 0:19
  • I've tried that too, various variations. I'm trying the two different action supports now actually. – RC82 Mar 11 '16 at 0:21
  • @BorisBachovski, Actually I tried with a sample page and looks like we cannot have IF condition for action property or may be I am doing something wrong. – javanoob Mar 11 '16 at 0:23
  • That could be the case. As I said I haven't tried with action. In that case, the other option with having 2 separate tags should be the way to go. – Boris Bachovski Mar 11 '16 at 0:25

you have to use the expression symbol only once at the beginning.. and all the following conditions and values doesn't need the expression symbol again.

<apex:actionsupport event="onchange" action="{!IF(x=1, save(), otherMethod())}"/>
| improve this answer | |
  • 1
    You beat me to it! :) – Boris Bachovski Mar 11 '16 at 0:10
  • Did you try this? It doesn't work actually. – javanoob Mar 11 '16 at 0:24

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.