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
inanskarinanskar 

How to insert multiple records for an Object

Hii all ,

I have the requirement that i have 5 pageblock section  such that each pageblocksection  contains the all feilds and the entire page contains only one save button such that i have to insert the all the fields

i'm giving the code can u get me the correct approach for the code

 

 

<apex:page controller="file2">
<apex:form >
<apex:pageBlock >

<apex:pageBlockButtons >
<apex:commandButton value="Save" action="{!save}" />
</apex:pageBlockButtons>

<apex:pageBlockTable value="{!f1}" var="f" id="form1">

<apex:column headerValue="Name">
<apex:inputField value="{!f.Name}"/>
</apex:column>
<apex:column headerValue="Description">
<apex:inputField value="{!f.Description__c}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockTable value="{!f1}" var="f" id="form2" >

<apex:column headerValue="Name">
<apex:inputField value="{!f.Name}"/>
</apex:column>
<apex:column headerValue="Description">
<apex:inputField value="{!f.Description__c}"/>
</apex:column>
</apex:pageBlockTable>

</apex:pageBlock>
</apex:form>
</apex:page>

 

 

controller

-----------------------------------

public with sharing class file2 {

public file2() {
f1 = new List<File__c>();
f1.add(new File__c());

}


public List<File__c> f1 {get; set;}

public PageReference save()
{
insert f1;

return null;
}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
ajmerakavitaajmerakavita

Hi

Using apex:repeat tag you can solve your problem.

In the constructor define the new instance for of your object , no. of times you want it.

Eg. -

f1 = new List<File__c>();
for(integer i=0;i<5;i++)
{
    f1.add(new File__c());
}  

 

On your page use Page Block Section with apex:repeat.

Eg -

<apex:repeat rows="5" value="{!f1}" var="f">
<apex:pageBlockSection title="Section" id="form1">
<apex:inputField value ----->

---------

</apex:pageBlockSection>
</apex:repeat>

All Answers

ajmerakavitaajmerakavita

Hi

Using apex:repeat tag you can solve your problem.

In the constructor define the new instance for of your object , no. of times you want it.

Eg. -

f1 = new List<File__c>();
for(integer i=0;i<5;i++)
{
    f1.add(new File__c());
}  

 

On your page use Page Block Section with apex:repeat.

Eg -

<apex:repeat rows="5" value="{!f1}" var="f">
<apex:pageBlockSection title="Section" id="form1">
<apex:inputField value ----->

---------

</apex:pageBlockSection>
</apex:repeat>

This was selected as the best answer
inanskarinanskar

yep i tried inserting of number of objects..IT worked fine..but my requirement is i have to use only single object through single object only i have to insert multiple records ...

can u help me through that ..

Thanks for ur reply..

inanskarinanskar

sry it's working for single object

thanks ajmerakavita