0

I have tried IF and ternary operator to conditionally assign a methods from 2 apex methods on a visual force page but didn't get any success.

I am using standard controller and custom extension my requirement is when account.type == 'a particular type' then action on a button should use the standard save method otherwise it use custom apex method which will render a pop up on vf page with certain values.

I have tried like below

<apex:commandButton value="save" action="{!IF(account.type == 'atype', !Save, !anApexMethod)}"/>

or

<apex:commandButton value="save" action="{!IF(account.type == 'atype', {!Save}, {!anApexMethod})}"/>

I also tried several other combination like above but didn't work.

creating 2 buttons using rendered will not work as I will be using this page to override the standard edit button.

Please suggest if it is possible to conditionally choose apex method on a button

Thank you

7
  • Hi. Please can you provide code snippets for the page and extension controller so your question can be answered?
    – Phil W
    Oct 25 '19 at 8:57
  • Thank you for your response Phil, I have updated the question
    – gs650x
    Oct 25 '19 at 10:56
  • No idea why you would do it that way - I cannot think of a way to make it work as you are trying. What do you really want to achieve there? I would try and make the actual decision within Apex code - so in anApexMethod go for if (account.type == 'atype') ... Oct 25 '19 at 12:50
  • "creating 2 buttons using rendered will not work as I will be using this page to override the standard edit button." Can you explain why this will not work? Even if you override the standard edit button, I can't see why it would affect conditional save buttons in the VF page. All the vf page needs to know is the standard controller and the Account type. Once it determines the Account type from that Type field, it should be able to use the standard save action provided you have give the VF page the standard controller. The edit override should not affect that in any way, as far as I can tell.
    – Roti
    Oct 25 '19 at 17:16
  • rendered thing doesn't work because when I hit the edit button this page will load and it will have the already saved value for type not the one that I change on the page, my scenario is to either save a record or a display a pop up to confirm save when type is equal to certain value.
    – gs650x
    Oct 26 '19 at 8:07
2

A trivial way to achieve this would be add both buttons to the page with conditional rendering:

<apex:commandButton value="save" action="Save" rendered="{!account.type == 'atype'}"/>
<apex:commandButton value="save" action="anApexMethod" rendered="{!account.type != 'atype'}"/>

Basically you either render it with Save OR you render it with anApexMethod based on mutually exclusive conditions.

UPDATE

According to your edited question you want this to change rendering when the account.type is updated. I assume you have a separate input for that on the page. In this scenario you need to set up the account.type input to have an actionSupport for onchange, perhaps something like this (I can't remember the exact way you relate the action support to the input so this may not be 100% accurate but gives the gist):

<apex:inputField value="{!account.type}">
    <apex:actionSupport event="onchange" rerender="saveButton"/>
</apex:inputField>

<apex:outputPanel id="saveButton" layout="none">
    <apex:commandButton value="save" action="Save" rendered="{!account.type == 'atype'}"/>
    <apex:commandButton value="save" action="anApexMethod" rendered="{!account.type != 'atype'}"/>
</apex:outputPanel>

An action support with no action does a page refresh, but since rerender is specified it restricts it to the elements with the specified IDs, here "saveButton", thereby refreshing the part of the page only that we want and leaving everything else alone.

6
  • rendered thing doesn't work because when I hit the edit button this page will load and it will have the already saved value for type not the one that I change on the page, my scenario is to either save a record or a display a pop up to confirm save when type is equal to certain value
    – gs650x
    Oct 26 '19 at 8:08
  • You need to look into the "rerender" options. You can make it such that entering a value can trigger rerendering of parts of the page. Honestly, this is the right approach, it just gets a bit more complex to add in the other stuff using rerender combined with other VF features.
    – Phil W
    Oct 26 '19 at 8:21
  • I think the fact you got a partial reply, btw, is because you gave a partial question with not enough background, not stating what you are really trying to achieve (the comment you added states this now) and not enough of your VF page and controller.
    – Phil W
    Oct 26 '19 at 8:23
  • My apologies Phil, I couldn't explain the question first, thank you so much for your updated answer, now your solution works but I have to hit save button twice to see the expected result, on first click it saves the record and reload the page and on 2nd save it does the appropriate action as per the type value, could you please suggest.
    – gs650x
    Oct 26 '19 at 15:44
  • Hi Phil, please let me know if is there any solution for clicking save button twice.
    – gs650x
    Oct 29 '19 at 12:38

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.