Executing some controller code before a default page is displayed

I want to track the id of an object displayed in some default UI. For a while I have been using this page and code:

<apex:page standardController="Claim__c" extensions="ClaimController" action="{!updateLastAccessed}">
    <apex:detail relatedList="true" relatedListHover="true"/>
</apex:page>
public with sharing class ClaimController {
    private Claim__c claim;
    public ClaimController(ApexPages.StandardController controller) {
        this.claim = (Claim__c) controller.getRecord();
    }
    public PageReference updateLastAccessed() {
        // Record the id (via a utility not detailed here) or execute whatever other code you want to
        UserUtil.lastAccessedClaimId = claim.Id;
        // Stay on current page
        return null;
    }
}

where the page is set to override the view action in the object’s “Standard Buttons and Links”. The result is that the id is tracked when the above overriding page invokes the updateLastAccessed controller method – good. But the apex:detail output while appearing identical to the default UI does not support the in-place editing that the default UI provides – bad as this leaves the user double clicking and wondering why the in-place editing does not work for this one page.

When outputting a link in a Visualforce page it is possible to force the destination to be the default UI page even if an override of the view action is in place – see e.g. Default UI page via URLFOR. The minor change below to the controller class is what I am now using. It copies the URL pattern used in the HTML output by URLFOR and gives the desired result of tracking the id and then using the default UI to present the data complete with in-place editing:

public with sharing class ClaimController {
    private Claim__c claim;
    public ClaimController(ApexPages.StandardController controller) {
        this.claim = (Claim__c) controller.getRecord();
    }
    public PageReference updateLastAccessed() {
        // Record the id (via a utility not detailed here) or execute whatever other code you want to
        UserUtil.lastAccessedClaimId = claim.Id;
        // Copy the pattern URLFOR uses to go to the default UI page
        PageReference pageRef = new PageReference('/' + claim.Id);
        pageRef.getParameters().put('nooverride', '1');
        return pageRef;
    }
}

Problem solved.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s