I want to create a VF page that displays a Custom Object's Lookup field. I create the object in my apex controller class using the new keyword and would like to add a lookup field to write another object into it like this:

<apex:inputField id="mapConstructie" value="{!project_element.Constructie__c}"/>


public Project_Element__c project_element { get; set; }
 project_element    = new Project_Element__c( project__c =
currentProject.Id );    
public void addEditProjectElement( )

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

Where Constructie__c is a lookup field. If I now do an insert, project_element.Constructie__c is empty. How can I make sure that project_element.Constructie__c is filled using the VF code above?

  • Adding required="true" to the apex:inputField should be a simple solution. – Keith C Oct 22 '14 at 14:50
  • I don't mean that the user should be forced to add a value; I mean that when they do, the field in the project_element object should be filled. Right now, if I do a insert project_element; it's Constructie__c field is empty, even when I just added a value using the lookup field. – Jur Van Oerle Oct 22 '14 at 14:53
  • Perhaps you should post your controller code then; the ID of the object would normally be automatically set in the project_element reference when the form is submitted. – Keith C Oct 22 '14 at 14:59
  • OK, I edited it, so my controller is in there now too. It's a very long file so I only added parts of it here. – Jur Van Oerle Oct 22 '14 at 15:04
  • Bit hard to tell, but as long as project_element is only initialised once (e.g. in the constructor) and you are using e.g. an apex:commandButton to post the form, I can't see anything wrong. Perhaps someone else can. – Keith C Oct 22 '14 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.