1

With objects that are saved in the database, Apex provides a very nice way to allow you to navigate to a standard detail or edit page. Consider the following:

public class CustomController{

    public PageReference navigateToDestination(){

        Account a = new Account(Name = 'Account Name');
        insert a;

        ApexPages.StandardController Controller = new ApexPages.StandardController(a);
        PageReference Destination = Controller.view(); // or .edit() 

        Destination.setRedirect(TRUE);
        return Destination;

    }

}

<apex:page controller="CustomController">
    <apex:form >
        <apex:commandButton action="{!navigateToDestination}" value="PressMe" />
    </apex:form>
</apex:page>

This works great when the account has been inserted, but fails when the account is not inserted. The debug log reveals that this page reference that is returned from the view() and edit() methods is dependent on the object's id, and therefore won't work if the object hasn't been inserted:

debug log

Does anyone know of a way that standard view or edit pages can be accessed without using an id so that they can be navigated to and populated with data on an object that has not been inserted into the database?

6
  • How can you view or edit a record that does not exist? You can navigate to the new page in that case, by typing the object prefix followed by /e - For new account: PageReference destination = new PageReference('/001/e'); Jul 28, 2015 at 3:32
  • @Bachovski It's not that it doesn't exist, just that it isn't yet in the database. The account a in my example is a real instance of an account object with field values populated. What I was hoping was possible was that salesforce could use the values from the account a as they are in apex and display them as they would appear on a standard page layout.
    – martin
    Jul 28, 2015 at 3:42
  • I get what you mean and I don't think there is a "standard" way to use standard layout for an object in memory. You'd have to build something custom for that I believe. Jul 28, 2015 at 3:46
  • I wouldn't think that is possible. Without inserting into database, how do you persist the data between execution contexts? There are some tricky ways to resolve this but none of them is standard and nothing is easier than inserting into database
    – Lance Shi
    Jul 28, 2015 at 5:01
  • @Bachovski Thanks for sharing your experience. I didn't think that Apex gave us that kind of control to work with standard page layouts, but I thought I'd run it by the community to make sure. If you re-post your comments as answers I'll go ahead and close this question out.
    – martin
    Jul 29, 2015 at 3:04

1 Answer 1

1

You can't use the standard functionality to view/edit record unless the record is saved to the database. If you wish to implement such a functionality to deal with objects in memory, then you'll have to build something custom in your VisualForce page.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .