Search code examples
salesforceapex-codevisualforce

How do I get a "New" button on my custom apex:pageBlockTable for Cases?


I have a custom Cases apex page where I use a apex:pageBlockTable to display my list of Cases. Here is the code for it:

<apex:page standardController="Case" recordSetVar="Case" sidebar="true" showHeader="true">
    <apex:form >
        <apex:pageBlock title="Cases">
              <apex:outputLabel value="View:"/>
              <apex:selectList value="{!filterId}" size="1">
                <apex:actionSupport event="onchange" rerender="cases_table"/>
                <apex:selectOptions value="{!listviewoptions}"/>
              </apex:selectList>
            <apex:pageBlock >
                <apex:pageBlockButtons >
                </apex:pageBlockButtons>
                <apex:pageBlockTable value="{!case}" var="c" rows="50" id="cases_table" >
                    <apex:column >
                        <a target="_parent" href="{!URLFOR($Action.Case.View, c.id)}">{!c.CaseNumber}</a>
                        <apex:facet name="header">Case Number</apex:facet>
                    </apex:column>
                    <apex:column value="{!c.ContactId}" />
                    <apex:column value="{!c.Subject}" />
                    <apex:column value="{!c.Status}" />
                    <apex:column value="{!c.Priority}" />
                </apex:pageBlockTable>
            </apex:pageBlock>
        </apex:pageBlock>
    </apex:form>
    <base target="_parent"/>
</apex:page>

I would like to get a button inside my apex:pageBlockButtons like in the default Cases page. When the user clicks the button, I would like it to take the user to the new Cases page. I tried this:

<apex:commandButton action="{!new}" value="New"/>

but that gave me an error. How do I make a button that will take me to the new Cases page?


Solution

  • Try this:

    <apex:commandButton onclick="window.location.href='{!URLFOR($Action.Case.NewCase)}'" value="New" immediate="true" rerender="blank"/>
    

    Important to note that the rerender tag is needed otherwise the page will do a postback. By using a dummy rerender tag the redirect will work. Immediate is there to avoid running any validation rules on click.