I have a visualForce page that contains an object which has a autonumber as name. When the user presses save, the object is stored and I want the name to be populated with the name of the object. I'm guessing that the autonumber is generated when the object is inserted into the DB but if I try to ApexPages.addMessage the name after it has been added to the DB, it says that I cant reference a null object, implying that the name is not set after it has been inserted.

Here is my code (in a nutshell):

public void addEditProjectElement( )
        elementsExist    = true;           
            project_element         = currentProject.Project_Element__r;
            ApexPages.Message msg;
            if ( project_elements_map.keySet( ).contains( currentProject.project_element__r.id ) )
                update project_element;
                msg = new Apexpages.Message( ApexPages.severity.INFO, project_element.Name + ' is updated.' );
                insert project_element;
                msg = new Apexpages.Message( ApexPages.severity.INFO, project_element.Name + ' is added.' );

            Apexpages.addmessage( msg );

            project_elements_map.put( project_element.Id, new ProjectElementDTO( project_element, project_elements_map.size( ) ) );
        } catch( Exception exc )
            ApexPages.addMessages( exc );

and here is my VisualFore page:

 <apex:outputPanel id="ProjectElementOutputPanel">
                <apex:pageblockSection id="ProjectElementInputError" collapsible="false" title="Bouw Elementen" columns="1">
                    <apex:messages styleClass="error, info"/>
                        <apex:outputLabel value="{!$ObjectType.Project_Element__c.fields.Name.label}"/>
                        <apex:outputLabel value="{!currentProject.project_element__r.Name}"/>
                    <apex:pageBlockSectionItem id="Project_Element__Element">
                        <apex:outputLabel value="{!$ObjectType.Project_Element__c.fields.Element__c.Label}"/>                        
                        <apex:inputField id="lookupElement__c_ID" value="{!currentProject.project_element__r.Element__c}"/>                          
                    <apex:pageBlockSectionItem id="Project_Element__Constructie">
                        <apex:outputLabel value="{!$ObjectType.Project_Element__c.fields.Constructie__c.Label}"/> 
                        <apex:inputField id="lookupConstructie__c_ID" value="{!currentProject.project_element__r.Constructie__c}" />                               
                    <apex:pageBlockSectionItem id="Project_Element__Materiaal">
                        <apex:outputLabel value="{!$ObjectType.Project_Element__c.fields.Materiaal__c.Label}"/>
                        <apex:inputField id="lookupMateriaal__c_ID" value="{!currentProject.project_element__r.Materiaal__c}"/>                          

                    <apex:pageblockSectionItem >

                        <apex:outputPanel >
                            <table width="100%" border="0" cellpadding="0" cellspacing="0">
                                    <td align="center">
                                        <apex:commandButton value="Add/Edit" action="{!addEditProjectElement}" reRender="ProjectElementInputError, ProjectElementenOupupt, ProjectElementOutputPanel" status="ProjectElementStatus"/>
                                        <apex:commandbutton onclick=
                                            return false;" 
                                            value="Clear" Status="ProjectElementStatus" reRender="ProjectElementOutputPanel,ProjectElementInputError"/>

1 Answer 1


By the time the DML completes, the value has been populated. However, this change isn't automatically returned back to the calling process. You have to perform a query to get the new auto-number value.

  • thanks :) Can I just do this then? insert project_element; project_element = [SELECT Name, Constructie__c, Element__c, Materiaal__c FROM Project_element__c WHERE Id =: project_element.Id]; msg = new Apexpages.Message( ApexPages.severity.INFO, project_element.Name + ' is added.' ); Or do I have to do it asynchronous? In other words, will insert always be completed when I enter the second LOC? Oct 23, 2014 at 14:07
  • No need for asynchronous callback. It'll be available when you query the record back in, as in your comment.
    – sfdcfox
    Oct 23, 2014 at 14:35

You must log in to answer this question.

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