You need to sign in to do that
Don't have an account?
Technos
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
and class: Define as public variables
All Answers
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.
Hi Raman,
Below is the sample code as per your requirement.
Visualforce Page:
Apex Class:
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
<apex:inputText value="{!obj1.field1}" id="input1"/>
Giving error "Unknown property testForSFDC.obj1" ??
On VF page
You have obj1 declared at COntroller? where? send the code snippet
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>
and class: Define as public variables
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