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
pjaenick.ax736pjaenick.ax736 

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"/>
            </apex:outputPanel>
        </apex:outputpanel>
    </apex:form>
</apex:page>

--------------------------------------------------------
public class MyCustomObjectExt {

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

    public MyCustomObjectExt(ApexPages.StandardController sc) {
        this.sc=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!
Abhi__SFDCAbhi__SFDC
In order to open any record you need to append the url with the specific id.

<you org instance>.salesforce.com/apex/VFPAGENAME?id=0019000000ogIhe

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.
pjaenick.ax736pjaenick.ax736
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
0000128

?? 

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,
Pete
pjaenick.ax736pjaenick.ax736
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 ? :)
Abhi__SFDCAbhi__SFDC
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='+thisrecord.id);
}

>> Let me know if it doesnt work.