0

I have a use case where I have a button "Add another". I want this button to be hidden until an apex method "Save" is called. The save method calls a javascript method which in turn displays the button.

Here is my visualforce code:

<b><apex:commandButton id="Save" styleClass="slds-button slds-button-- 
neutral btn-lg" value="Save" action="{!saveRecord}" rerender=""/></b>


<div id="addnew" style="display:none;">

<b><apex:commandButton styleClass="slds-button slds-button--neutral btn-lg" 
action="{!AddNew}" value="Add Another" immediate="true" /></b>

<br></br>

</div>

<script>

  function func()

   {


    $("#addnew").show();

   }

<script>

Apex Class:

public string callfunc{get;set;}
public pagereference saveRecord() {


     ................ 
     ................
      callfunc= '<script> func(); </script>';


      return null;

      }
1

Thats not the way, javascript methods are getting called.

Your use case can be solved this way.

Controller

Create a Boolean property as follows.

public Boolean displayAddNew {get;set;}

In the saveMethod() make it true. (displayAddNew = true).

Visualforce code needs to be changed as follows:

Render Add New Button based on {!displayAddNew}

In the save button rerender="addNewBtnPnl"

<apex:commandButton id="Save" styleClass="slds-button slds-button-- 
neutral btn-lg" value="Save" action="{!saveRecord}" rerender="addNewBtnPnl"/>


<apex:outputPanel id="addNewBtnPnl" style="display:none;">

<b><apex:commandButton rendered="{!displayAddNew }"  
styleClass="slds-button slds-button--neutral btn-lg" 
action="{!AddNew}" 
value="Add Another" 
immediate="true" /></b>
</apex:outputPanel>
  • one thing to note about the original markup, using immediate=true on the command button causes form data not to be posted and may cause some undesired side effects with the data being captured by the input fields on the page – Mark Pond Aug 10 '18 at 19:04
  • correct you are, thats why I think, it needs to be wrapped with actionRegion – Santanu Boral Aug 10 '18 at 19:06

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.