[SalesForce] How to access the standard controller without passing the ID parameter in the URL

How can I use the standard controller to access fields on a record in a visualforce page without passing the ID in the URL?

Example: Instead of "https://c.xxx.visual.force.com/apex/abc?id=a0890010008dQTE", I want to use "https://c.ap1.visual.force.com/apex/tabbed".

Best Answer

Even if you are using a StandardController, you can overload the internal id in an extension:

public with sharing class AbcExtension {

    public AbcExtension(ApexPages.StandardController controller) {
        //this will work just as if you'd handed it in as part of the url 
        ApexPages.currentPage().getParameters().put('id', 'a0890010008dQTE');
    }
}

Alternately you can use another parameter (such as name) to pass in the url

public with sharing class AbcExtension {

    public AbcExtension(ApexPages.StandardController controller) {
        // get the parameter from the url
        String otherParameter = ApexPages.currentPage().getParameters().get('otherParameter');

        // query the object
        Abc__c theObject = [SELECT ID, .... FROM Abc__c WHERE Some_Field__c = :otherParameter LIMIT 1];

        // You'll need to add in some error handling code..

        // this will set the Id, so now you can use the standard controller and just reference fields on the page
        ApexPages.currentPage().getParameters().put('id', theObject.Id);
    }
}

And in your page:

<apex:page standardController="Abc__c" extensions="AbcExtension" />