function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

VF page: returning the record name after insert

I'm trying to display the record 'name' field (auto-numbered field) on a visualforce page after insert

How can I do that?  Do I really need to re-query for it to display it after insert?

Here's my page & controller class :
<apex:page standardController="MyCustomObject__c" extensions="MyCustomObjectExt">

    <apex:form >
        <apex:outputPanel id="all">
            <apex:sectionHeader title="My Custom Object" subtitle="{!MyCustomObject__c.Name}"/>  <!-- 'Name' is always null :(  -->
            <apex:outputPanel >
                <apex:commandButton action="{!SaveButton}" value="Save Me!" status="SaveButtonStatus" rerender="all"/>

public class MyCustomObjectExt {

    private MyCustomObject__c thisRecord;
    private ApexPages.StandardController sc = null;

    public MyCustomObjectExt(ApexPages.StandardController sc) {;
        thisRecord = (MyCustomObject__c) sc.getRecord();

    public PageReference saveButton() {
        try {
            Upsert thisRecord;
            string s = '/' + ('' + thisRecord.get('Id'));
        } catch (Exception e){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'Whoops !'));
        return null;

Thank you!
In order to open any record you need to append the url with the specific id.

<you org instance>

So if you are opening this page via button in your button url you need to append the ID, you can get the ID through AJAX  toolkit or through a soql Query. It depends from where or when you would like to show this Page.
Apologies - I think I left out some information in my initial post (thanks for you reply) -

I've created a custom form in support of an existing custom object, with a controller extention class, (shown above).  Here's the stuff I neglected to state:

  - From the custom object's list-view, I'm clicking the [New] button.
  - The custom VF page is presented, with a title of "My Custom Object" as well as the record 'Name' value (currently null as the record hasn't been inserted yet).
  - I click the [Save Me!] button, and the record is successfully inserted.
  - The record "Name" is an auto-numbered field, so the Name field is set to 0000128 (for example) in the database.
  - The [Save Me!] button includes a rerender="all" parameter

Why doesn't the page re-render with the heading of :

My Custom Object


Do I need to :
   A)  re-query to get the name field
   B)  use "return new Pagereference(s);" following the upsert, or
   C) is there a better / preferred way of getting the record name?

Thanks for any assistance,
choice (B) should have said :

B) use "return new Pagereference('/' + ('' + thisRecord.get('Id'))); " following the upsert, or

geez... can't edit / correct posts any longer ? :)
Thanks for sharing the complete scenario.

Right after upserting thisRecord (Account variable) in your controller in SaveButton method .
Use -:
public PageReference saveButton() {

            Upsert thisRecord;
return new PageReference('<your-SFDC-url>/apex/<Name-of-VF-Page>?id=';

>> Let me know if it doesnt work.