[SalesForce] How to stop refreshing the entire VF page when command link is clicked

When a command link is clicked on page it is refreshing the whole page and asking to enter require fields. But I want to enter required fields only after clicking the command link.

Note:Command link is used to open a new VF page

VF Page:

                <apex:inputField value="{!Account.Name}" required = true>

                   </apex:inputField>

              <apex:outputPanel >
              <apex:outputField  value="{!Account.Input__c}" />
             <apex:commandLink  action="{!redirectToVFPage}" target="_blank">
</apex:commandLink>
             </apex:outputPanel>

Best Answer

You have two things going on here:

  1. Refresh - Without rerender the entire page is reloaded
  2. Required fields - If the field is required it must be populated when there is no rerender

Here is the most basic example to help you out, put it in your org and play with it a bit to see the effect of different options.

Class

public class myExample{

    public Account acc {get; set;}

    public myExample(){
        acc = New Account();
    }
    public void updateCurrentRecord(){
        //Do some stuff        
    }

}

VF Page

<apex:page id="myExamplePage" controller="myExample">
    <apex:pageMessages id="msgs"/>
    <apex:form >
        <apex:commandLink action="{!updateCurrentRecord}" reRender="thePanel, msgs" value="Click me"/>
            <apex:inputField value="{!acc.Name}" required="true"/>

        <apex:outputpanel layout="block" id="thePanel">

            {!acc.Name}

        </apex:outputpanel>


    </apex:form>


</apex:page>

Note you need to add the Page Messages component to see the required validation error when using rerender. Also, displaying the error at the field level requires you to NOT use the rerender attribute.

Its all a bit confusing.....

This will not refresh the page but will refresh the output panel to show the update. You could leave the rerender=none and debug the values in the controller and see that it works.

If you remove the rerender from the command link, you will see your issue return.

Using Immediate=true will not send the value to the controller and the thePanel panel will be blank on rerender.

  • If used without rerender - Page will refresh and your values will not save
  • If used with the rerender - Page will not refresh and values will not be saved

Another item to review would be action regions, but may not be applicable in your use case. If you need to submit part of the form and not the others (having required fields) look into that.

Only use immediate=true when you want to cancel any inputs and do the action immediately - think a cancel redirect. If you want to open your new page without accepting any inputs the user entered then do use immediate=true

Related Topic