3

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? – BritishBoyinDC Sep 26 '13 at 22:31
  • No, no relationship. – Caky Sep 26 '13 at 22:36
  • It's unclear what you're asking, can you please be more specific and provide more information? – Boris Bachovski Sep 26 '13 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 Sep 26 '13 at 22:53
11

your VF Page

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

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

</apex:pageblock>
</apex:page>

Controller

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 Sep 27 '13 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 – Chris Duncombe Sep 27 '13 at 17:55
7

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 Sep 26 '13 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 Sep 27 '13 at 0:32

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.