[SalesForce] Creating a Task and staying on the detail page for that task (not navigating elsewhere)

When you click the New Task or Log A Call button from the Open Activities or Activity History related list on e.g. an Opportunity, you're taken to the regular https://<instance>.salesforce.com/00T/e URL, ready for your input on the details of the Task.

On this page, when you click Save, you're being redirected back to the Opportunity you came from (to which the new task now belongs).

We have a custom button on Tasks that we want people to click, in certain situations, right after having created the Task. This, of course, makes it rather cumbersome for the user as they now have to:

  • Click New Task
  • Fill out the task info
  • Click Save, which redirects you to the Opportunity
  • Navigate to the related lists (Open Activities or Activity History) and locate the newly created task
  • Click the task to get to the detail page of it
  • Finally click our custom button

Is there any way to force the navigation to stay on the detail page of the task when you create it? I'm thinking this is how everything else works in SFDC – when you create a new Account and you click Save, you're brought the detail page of this new account. Not for tasks, it seems…

I've been trying to figure out if there's any form of URL parameter you could utilize (retUrl, saveUrl etc) but none of them seem to let you stay on the record (since you don't know the task's ID yet). Leaving retUrl out altogether simply redirects you back to ..../home/home.jsp which doesn't help either.

Best Answer

It's a bit of a hack, but you could create a Visualforce page and controller action that will redirect the user to the most recently created Task for a record.

Set the &retURL going into the task creation to send the user to this new VF page and include the parent opporutnity record ID in the query string.

/00T/e?what_id={!Opportunity.Id}&retURL=%2Fapex%2FredirectLastTask?rId= {!Opportunity.Id}

public with sharing class redirectTaskController {
    string recordId = '';

    public redirectTaskController() {
        recordId = ApexPages.currentPage().getParameters().get('rId');
    }

    public PageReference redirectToTask() {
        PageReference activityPage = new PageReference('/' + recordId);

        Task[] recentTasks = [SELECT Id, CreatedDate 
                              FROM Task WHERE WhatId = :recordId 
                              ORDER BY CreatedDate DESC LIMIT 1];

        // Check that the new Task if fairly recent.
        if (recentTasks.Size() == 1 && 
                recentTasks[0].CreatedDate.addHours(1) > Datetime.now()) {
            activityPage = new PageReference('/' + recentTasks[0].Id);
        }

        activityPage.setRedirect(true);
        return activityPage;
    }
}

Visualforce Page

<apex:page controller="redirectTaskController" action="{!redirectToTask}" >
    <p>Redirecting to task</p>
</apex:page>

Another idea would be to create a Visualforce page that uses the Task standardController and provides a custom save action.

Related Topic