2

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;           
        try
        {           
            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.' );
            }
            else
            {
                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:PageBlockSectionItem>
                        <apex:outputLabel value="{!$ObjectType.Project_Element__c.fields.Name.label}"/>
                        <apex:outputLabel value="{!currentProject.project_element__r.Name}"/>
                    </apex:PageBlockSectionItem>
                    <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>
                    <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>
                    <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:pageblockSectionItem >

                        <apex:outputPanel >
                            <table width="100%" border="0" cellpadding="0" cellspacing="0">
                                <tr>
                                    <td align="center">
                                        <apex:commandButton value="Add/Edit" action="{!addEditProjectElement}" reRender="ProjectElementInputError, ProjectElementenOupupt, ProjectElementOutputPanel" status="ProjectElementStatus"/>
                                        <apex:commandbutton onclick=
                                            "document.getElementById('{!$Component.Project_Element__Element.lookupElement__c_ID}').value='';
                                            document.getElementById('{!$Component.Project_Element__Constructie.lookupConstructie__c_ID}').value='';
                                            document.getElementById('{!$Component.Project_Element__Materiaal.lookupMateriaal__c_ID}').value='';
                                            return false;" 
                                            value="Clear" Status="ProjectElementStatus" reRender="ProjectElementOutputPanel,ProjectElementInputError"/>
                                    </td>
                                </tr>
                            </table>
                        </apex:outputPanel>                    
                    </apex:pageblockSectionItem>
                </apex:pageblockSection>
            </apex:outputPanel>
2

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? – Jur Van Oerle Oct 23 '14 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 '14 at 14:35

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.