function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
di_zoudi_zou 

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?

Best Answer chosen by Admin (Salesforce Developers) 
Alex.AcostaAlex.Acosta

According to this post, you need to modify it.... http://boards.developerforce.com/t5/General-Development/Output-Link-to-create-a-new-Case/td-p/274935

 

<apex:commandButton value="New" action="{!URLFOR($Action.Case.NewCase)}" />

All Answers

admintrmpadmintrmp

You're supposed to contain pageBlockButtons in a single un-nested pageBlock instance.

 

<apex:pageBlock>
    <apex:pageBlockButtons>
<apex:commandButton value="{!save}" /> </apex:pageBlockButtons> </apex:pageBlock>

 

I don't believe there is a way to create buttons for a table header using Visualforce.

di_zoudi_zou

I can create this without any errors:

 

        <apex:pageBlock title="Cases">
            <!-- Stuff -->  
            <apex:pageBlock >
                <apex:pageBlockButtons >
<apex:commandButton action="{!save}" /> </apex:pageBlockButtons> <apex:pageBlockTable value="{!case}" var="c" rows="50" id="cases_table" > <!-- Stuff --> </apex:pageBlockTable> </apex:pageBlock> </apex:pageBlock>

 

I'm looking for a way to get a "New" button instead of a "Save" button. 

 

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

When I do that however, I get this error:

 

Error: action="{!new}": Unknown method 'CaseStandardController.new()'

admintrmpadmintrmp

The code I posted was purely an example of how you should be writing your code.

 

New is not a method you have written, nor is a standard method. You will need to write some Apex code to ensure that button works.

 

Create a method in your Apex Class and call it new(). You will then be able to save your page.

Alex.AcostaAlex.Acosta

Looks like you'll have to make an extension class...

There's currently no method for a "new" record according to the docs... http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_std_actions.htm

 

What you can do if you want to redirect them to create a 'new' record is do something like the following within your extension class

 

Public PageReference newRecord(){
     PageReference pr = new PageReference('/' + Case.getSObjectType().getDescribe().getKeyPrefix() + '/e?');
     pr.setRedirect(true)
     return pr;
}

 

 

admintrmpadmintrmp

A better way of doing is to leave Apex out of it and use URLFOR() within your Visualforce tag to send the user to the page.

 

Something like this should work:

 

<apex:commandButton value="New" action="{!URLFOR($Action.Case.New)}" />

 

di_zoudi_zou

When I do:

 

<apex:commandButton value="New" action="{!URLFOR($Action.Case.New)}" />

I get this error:

 


Error: Field $Action.Case.New does not exist. Check spelling 

 

Where can I find what methods are in $Action.Case?

Alex.AcostaAlex.Acosta

According to this post, you need to modify it.... http://boards.developerforce.com/t5/General-Development/Output-Link-to-create-a-new-Case/td-p/274935

 

<apex:commandButton value="New" action="{!URLFOR($Action.Case.NewCase)}" />
This was selected as the best answer
di_zoudi_zou

Awesome, thank you!

admintrmpadmintrmp

That is just frustrating... well done on the find.

di_zoudi_zou

It was really frustrating that I cound't find a list of those functions for the Case. It's also really frustrating that the function would be different for different models. 

 

Why couldn't they keep: 

"{!URLFOR($Action.Account.New)}"

the same for cases:

"{!URLFOR($Action.Case.New)}"

Why did they ahve to change it to:

"{!URLFOR($Action.Case.NewCase)}"
admintrmpadmintrmp

I would imagine because Case is a reserved word for programming with. They may not want to confuse it with Select Case.

 


Salesforce docs do not provide documentation for the Action properties, but you can find more information here on how to use them:

http://salesforcesource.blogspot.co.uk/2008/12/urlfor-function-finally-explained.html

 

The properties are exactly the same for all objects.