I'm trying to figure out how to insert or display records into two custom objects from one visualforce page. My objects are unrelated.

Object A (Id, Name), Object B (Color, Location)

Thank you for your helps in advance.

  • 1
    Is there a relationship between A & B? Commented Sep 26, 2013 at 22:31
  • No, no relationship.
    – Caky
    Commented Sep 26, 2013 at 22:36
  • It's unclear what you're asking, can you please be more specific and provide more information? Commented Sep 26, 2013 at 22:42
  • I have one VF page that requests users to provide their inputs such as ID and Name and they should be saved in Object A. On the same VF page, the second section requests users to input Color and Location and they should be saved to Object B. How can this be done?
    – Caky
    Commented Sep 26, 2013 at 22:53

2 Answers 2


your VF Page

<apex:page controller="myController">
          <apex:commandButton value="Save" action="{!saveObjects}"/>
          <apex:inputfield value="{!myA__c.Name}"/>
          <!-- add any other fields you want for this object -->

          <apex:inputfield value="{!myB__C.Color__c}"/>
          <apex:inputfield value="{!myB__C.Location__c}"/>



public class myController{

     public ObjectA__c myA      {get;set;}
     public ObjectB__c myB      {get;set;}

     public myController(){
          myA = new ObjectA__c();
          myB = new ObjectB__c();

     public void saveObjects(){
          insert myA;
          myB.ObjectA__c = myA.Id;
          insert myB;

  • Hi Dunc44, if my objectA (1) and objectB (Many) have a lookup relationship. How will the structure above be different?
    – Caky
    Commented Sep 27, 2013 at 16:49
  • 1
    You would have to insert A, then set the value of B.A to the newly created A objects ID. I updated code above Commented Sep 27, 2013 at 17:55

Caky, you need to write a custom controller/controller extension to do this, using a standard controller will only allow you to work with one object type if they're not related.

A very basic extension might look like this:

public with sharing class MyExtension
    ApexPages.StandardController sc;
    public ObjectB__c objB {get; set;}

    public MyExtension(ApexPages.StandardController sc)
        this.sc = sc;
        objB = new ObjectB__c();

    public ApexPages.PageReference SaveBoth()
        insert objB;
        return sc.Save();

In your page you would reference the first object as normal, and also objB, but instead of calling the regular Save() action you then call your own custom action in the extension which will save both records:

<apex:page standardController="ObjectA__c" extensions="MyExtension">
    <!-- snip -->
    <apex:inputField value="{!ObjectA__c.Name}"/>
    <apex:inputField value="{!objB.Name}"/>
    <apex:commandButton action="{!SaveBoth}" value="Save"/>
  • Thank you LaceySnr. I'll give it a go and I'll let you know.
    – Caky
    Commented Sep 26, 2013 at 23:23
  • Thank you to LaceySnr and Dunc44 for helping out. I can now move on to the next step in creating my VF page.
    – Caky
    Commented Sep 27, 2013 at 0:32

You must log in to answer this question.

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