I have an unauthenticated VF page that displays some information from the parent record. Below that I have a bunch of textboxes where users enter child object specific information and click on Save, which should create a new child record associated with the parent. The URL will contain the id of the parent record.

The way I am doing this now is:

Created a VF page with a bunch of apex:outputField to display the parent information.

Created a lot of apex:inputText to capture the user input related to the child.

Created a custom controller with a lot of public String field_id_on_the_form{get; set;} and then doing child_object.field__c = field_id_on_the_form;

This works. The problem is that there are a lot of fields for the child record (about 120. So I have 120 lines of {get;set;} and 120 lines of assigning the form values back to the child object. Not to mention I haven't yet implemented field validations yet.) and I feel that there should be a better way to do this. I am new to the Salesforce world, so wondering what the best way to implement this scenario is.

I think I can't use Standard Controller because the id in the URL belongs to the parent. Most of the user input on the form belongs to the child. So? What is the best way to do this if I have 120 fields on the form that need to be validated/captured before creating the child record?

Any high level code snippets would be nice. Thank you!


You don't need to use standard controllers for binding to a record. You can bind to a variable that represents w record, like:

Public MyObject__c myObject { get; set; }

Then, in your page, use normal field bindings, like thus:

<apex:inputField value="{!myObject.myField}" />

Be sure your guest profile has access to the object and field.

Edit: The value has to be instantiated through a constructor, or you'll get a NullPointerException.

public MyClass() {
    myObject = new MyObject__c();

You could also use lazy instantiation, but I don't prefer verbose code to simplistic code.

  • I get "System.NullPointerException: Attempt to de-reference a null object " when I try to do myObject.myField = "Test"; What am I missing? – Lightning Evangelist Jan 22 '15 at 21:37
  • @user3207927 Added a note for you. – sfdcfox Jan 22 '15 at 21:39

Using a controller extension or custom controller, instantiate a new ChildObject__c in the controller and expose it's "fields" on the VF page. In the constructor (or other initialization code), set the foreign key field to the ID of the parent record: ChildObject__c.ParentId__c = {id in url}.

Then when user submits form, simply insert the ChildObject__c instance.

  • Your answer implied the same thing as the other post but I picked the other because it had some code snippet in it. Truly appreciate your response! – Lightning Evangelist Jan 23 '15 at 13:32

Your Answer

