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>