7

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".

  • whats the use case here? is it just asthetics? security? – PartOfTheOhana Dec 18 '13 at 14:01
  • simply want to display record details from an ID. – MnZ Dec 18 '13 at 14:04
8

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" />
  • then won't this code only work for a single record- what ever that ID is? anyway to make it more scalable? – PartOfTheOhana Dec 18 '13 at 14:01
  • Yep. I suspect @MnZ is using it for a Site or such and just wants a clean URL. – bigassforce Dec 18 '13 at 14:03
  • yes @user320, you are right. But I will also want to try what nivyaj has suggested. Any thoughts on that? – MnZ Dec 18 '13 at 14:06
  • 2
    Sure, you can use the Site.UrlRewriter interface to clean them up. Maybe clarify in a separate question? – bigassforce Dec 18 '13 at 14:08
  • 1
    Thanks everyone. @user320 your suggestion is indeed very handy :) – MnZ Jun 16 '14 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.