+ Start a Discussion

how to add standard button (for another object) to a page & data order of pageBlockTable

I've overridden the standard case view with a visualforce page.
I've got a list of tasks in that page that relate to the case.

I'd like to add a standard button to add a new task (just like on the normal "open activities" detail page).

What's the best way to do this?

Also how can I re-order the data shown in a pageBlockTable (ordered by one of the displayed columns in this case)?

Code below:

<apex:page standardController="Case" showHeader="true" sidebar="true" tabStyle="Case"> 
.activeTab {background-color:#ef2b2d;color:white;background-image:none;}
.inactiveTab {background-color:lightgrey;color:black;background-image:none;}
<apex:tabPanel switchType="client" selectedTab="tabdetails" id="CaseTabPanel" tabClass="activeTab" inactiveTabClass="inactiveTab">
        <apex:tab label="Details" name="CaseDetails" id="tabdetails"> 
                <apex:detail relatedList="false" title="true"></apex:detail> 

                <apex:pageBlock title="Tasks">
                    <apex:pageblockTable value="{!Case.Tasks}" var="s" columns="3">
                        <apex:column width="100">
                            <apex:outputField value="{!s.DateForTimeRecording__c}" />
                        <apex:column width="120">{!s.Owner.Name}</apex:column>
                        <apex:column >{!s.Subject}</apex:column>
                        <apex:column breakBefore="true">&nbsp;</apex:column>
                        <apex:column colspan="2">{!s.Description} </apex:column>
        <apex:tab label="Open Activities" name="OpenActivities" id="tabOpenAct">
                <apex:relatedList list="OpenActivities" pageSize="20"></apex:relatedList> 


Hi Jeremy,

I'm not a Salesforce expert, just another developer, but these are the options i'm aware of:

Task Button:
  • The most rough and ready way to do this is by observing the existing behavior and trying to mimic it. ie see what shows up in the query string parameters when you click on an existing new task button.
  • The probably better advised way to go is to either create a method that creates your task (see page 221 in the APEX rdeveloper's guide) if you don't need additional user input, or have the button call a method that returns a PageReference and redirect to a standard task creation form or your own wizard.
Re-ordering Record set

  • If you want to order your data by a specific field and you're using SOQL to retrieve the data, you can just include the ordering specifications in your SOQL call (see SOQL reference http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_calls_soql.htm).
  • If you're trying to open this ordering up to the user, try working with the Map object, which can return a keyset that can then be ordered. Depending on the number of field options and the size of you record set, you may want to look into dynamic apex for setting up this map object or you may just want to set up the different map objects explictly. Someone from salesforce can probably give you better insight into the memory and processing ramifications of this...

Thanks for your reply.

1. I've found the existing "new" URL for a task and copied that. It works fine. I may do the APEX replacement at some point but more likely replace the new task page with a visualforce page. Link code is:
<apex:form >
  <apex:commandButton action="/00T/e—what_id={!Id}&retURL=/{!Id}" value="New Task" id="newTaskButton"/>

2. The ordering of items in the pageBlockTable:
- what I was trying to do was just a simple re-order of the data using the built in ability to specify a collection in the tag:
<apex:pageblockTable value="{!Case.Tasks}" var="s" columns="3">

 rather than having to write an APEX class to just get the data and sort. But it looks like that's the only way?

3. A new problem to do with parameters now, I've got another button to create an email and I'm passing parameters to this (it's the same page as the earlier code):
<apex:commandButton action="/_ui/core/email/author/EmailAuthor?p2_lkid={!case.ContactId}&rtype=003&p3_lkid={!Id}&retURL=/{!Id}&p6=(Case%20{!case.CaseNumber})" value="New Email" id="newEmailButton"/>

However, I get an error when this button is clicked (seen in a new page):
System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: ContactId

I've tried "case.Contact.Id" and the error is the same (but field shown is "Contact").

According to the documentation, this should be fine as the controller is "Case" and to get to the vars using lowercase object name. Maybe the case.ContactId is not being interpreted until the next page and then it's not valid?

Any ideas?


I've replaced the apex:commandButton with outputLink and it works OK:

<apex:outputLink value="/00T/e—what_id={!Id}&who_id={!case.ContactId}&retURL=/{!Id}" id="createNewTaskButton"><span class="button">Create New Task</span></apex:outputLink>
<apex:outputLink value="/_ui/core/email/author/EmailAuthor–p2_lkid={!case.ContactId}&rtype=003&p3_lkid={!Id}&retURL=/{!Id}&p6={!case.Subject}%20(Case%20{!case.CaseNumber})" id="createNewEmailButton"><span class="button">Create New Email</span></apex:outputLink>

But I had to put the span around the text to make it look like a standard button.

I'd be interested to know what the recommended way to do what I've done is, as I think the commandButton problem is just that it's submitting to the page controller and then doesn't process the parameters as I expected.
Maybe urlfor is supposed to help in somehow?

Yea you are right. Recommended way is to use urlfor, and thats because hardcoded links may break if salesforce changes the urls.
For example,


However, looks like visualforce is not recognizing NewTask as valid action right now. I have logged it as a bug. For now you can use your workaround.
{!urlfor($Action.Activity.NewTask)}  This is not working in VF right now


Message Edited by ES on 06-17-2008 09:25 AM
Thanks for checking. I'm glad it wasn't me then!

I'm interested to know how bugs are dealt with: do they get fixed mid release or do they all get batched up and added to the next release?

It really depends on the bug -- if it's easy to fix or if it's an unexpected change in behavior we might release it in a patch to the current release.  If there is a good workaround chances are it will have to wait for the following release.

As far as I can tell the {!urlfor($Action.Activity.NewTask)} functionality is not usable, most likely because it is not document well enough.

It works, but it is not clear on how the parameters should be passed.  For 3 years I have been using the workaround and salesforce seems to have just changed the urls so now my url's are broken.


Can't we just get the proper solution working correctly?  Has anyone got an answer?

I would need to pass the id of the parent record, and the recordtype of the task I want to create, and the returl.