function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
TechnosTechnos 

Save Multiple objects records from One VF page

I have custom objects A,B,C,D which has look up relation in another object E. Now I want to create one VF page in which all all the fields of each object will come and on click of save all record will be saved. Means on one VF page I wnt to save multiple objects record. I know that I need to create the controller. Please explain in detail with code.

 

regards

Raman

Best Answer chosen by Admin (Salesforce Developers) 
deepabalisfdcdeepabalisfdc
VF Page

<apex:page  controller="ExtentionController" >
    <apex:form >
          <apex:pageblock title="Hello {!$User.FirstName}!" >
              <h1>Congratulations</h1>
              This is your Accession Page !!<p/>
              <apex:pageBlockSection >
                  <apex:inputField value="{!objPatient.FirsName__c}"/>
       
              </apex:pageBlockSection>
              <apex:pageBlockButtons >
                  <apex:commandButton action="{!save}" value="Go"/>
              
                  <apex:commandButton action="{!cancel}" value="Cancel">
                  </apex:commandButton>
              </apex:pageBlockButtons>
          </apex:pageblock>
      </apex:form>
</apex:page>

 and class: Define as public variables

public class ExtentionController
{
    public Patient__c objPatient {get;set;}
   
    public Case_Accession__c objAccession {get;set;}
    public ExtensionController(){
     objPatient = new Patient__c();
     objAccession = new  Case_Accession__c();
    }
    public void save()
    {
       try
       {
          Insert objPatient;
          objAccession.Patient__c = objPatient.id;
          Insert objAccession;
       }
       catch(Exception ex){
          System.debug('\n\nException ='+ex.getMessage()+'\n\n');
       }
    }
public void cancel(){} }

 

All Answers

deepabalisfdcdeepabalisfdc

Raman,

Say you have 2 input fields in VF page. All fields will be bind with fields of different 2 objects.A  Third object has look up with other 2 objects.

<apex:page controller="testForSFDC">
  <!-- Begin Default Content REMOVE THIS -->
  <h1>Congratulations</h1>
  This is your new Page
  <apex:form >
  <apex:inputText value="{!obj1.field1}" id="input1"/>
  <apex:inputText value="{!obj2.field1}" id="input2"/>
  <apex:commandButton action="save()" value="Go"/>
  </apex:form>
  <!-- End Default Content REMOVE THIS -->
</apex:page>


Now in Apex controller testForSFDC, You have to initiate 2 objects .

ObjectA  obj1 = new ObjectA();
ObjectB  obj2 = new ObjectB();
ObjectC obj3 = new ObjectC();


In save button's method:-
public void save(){
   try{

   Insert obj1;

   Insert obj2;

  obj3.lookupfieldwithobj1 = obj1.id;

  obj3.lookupfieldwithobj2 = obj2.id;

   Insert obj3;

   }

catch(Exception ex){ ...}


}

I have not run, concept will be this.

hitesh90hitesh90

Hi Raman,

 

Below is the sample code as per your requirement.

 

Visualforce Page:

<apex:page controller="ctrlInsertNewRecords">
	<apex:form>
		<apex:commandbutton value="{!insertMultipleRecord}" value="Insert Multiple Records"/>
	</apex:form>
</apex:page>

 

Apex Class:

public class ctrlInsertNewRecords{
    public ctrlInsertNewRecords() {

    }
    public void insertMultipleRecord(){		
		E objE = new E();		
        insert objE;
		
        A objA = new A();
		objA.E__c = objE.id;
        insert objA;
        
		B objB = new B();
		objB.E__c = objE.id;
        insert objB;
		
		C objC = new C();
		objC.E__c = objE.id;
        insert objC;
		
		D objD = new D();
		objD.E__c = objE.id;
        insert objD;        
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.
 
Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator

 

 

TechnosTechnos

<apex:inputText value="{!obj1.field1}" id="input1"/>

 

Giving error "Unknown property testForSFDC.obj1"  ??

 

On VF page

deepabalisfdcdeepabalisfdc

You have obj1 declared at COntroller? where? send the code snippet

TechnosTechnos

Controller

 

public class ExtentionController
{
    Patient__c objPatient = new Patient__c();
   
    Case_Accession__c objAccession = new Case_Accession__c();
   
    public void save()
    {
       try
       {
          Insert objPatient;
          objAccession.Patient__c = objPatient.id;
          Insert objAccession;
       }
        catch(Exception ex){ }
    }
   
}

 

 

VF Page

<apex:page  controller="ExtentionController" >
    <apex:form >
          <apex:pageblock title="Hello {!$User.FirstName}!" >
              <h1>Congratulations</h1>
              This is your Accession Page !!<p/>
              <apex:pageBlockSection >
                  <apex:inputField value="{!objPatient.FirsName__c}"/>
       
              </apex:pageBlockSection>
              <apex:pageBlockButtons >
                  <apex:commandButton action="save()" value="Go"/>
              
                  <apex:commandButton action="{!cancel}" value="Cancel">
                  </apex:commandButton>
              </apex:pageBlockButtons>
          </apex:pageblock>
      </apex:form>
</apex:page>

deepabalisfdcdeepabalisfdc
VF Page

<apex:page  controller="ExtentionController" >
    <apex:form >
          <apex:pageblock title="Hello {!$User.FirstName}!" >
              <h1>Congratulations</h1>
              This is your Accession Page !!<p/>
              <apex:pageBlockSection >
                  <apex:inputField value="{!objPatient.FirsName__c}"/>
       
              </apex:pageBlockSection>
              <apex:pageBlockButtons >
                  <apex:commandButton action="{!save}" value="Go"/>
              
                  <apex:commandButton action="{!cancel}" value="Cancel">
                  </apex:commandButton>
              </apex:pageBlockButtons>
          </apex:pageblock>
      </apex:form>
</apex:page>

 and class: Define as public variables

public class ExtentionController
{
    public Patient__c objPatient {get;set;}
   
    public Case_Accession__c objAccession {get;set;}
    public ExtensionController(){
     objPatient = new Patient__c();
     objAccession = new  Case_Accession__c();
    }
    public void save()
    {
       try
       {
          Insert objPatient;
          objAccession.Patient__c = objPatient.id;
          Insert objAccession;
       }
       catch(Exception ex){
          System.debug('\n\nException ='+ex.getMessage()+'\n\n');
       }
    }
public void cancel(){} }

 

This was selected as the best answer
CoolSurenCoolSuren

Hi,

 

  Refer the following link, you can see one example related to your requirement. 

 

http://www.salesforce.com/docs/developer/cookbook/Content/vf_wizard.htm