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
NANCY1NANCY1 

How to create multiple records using VF page??

Hi,

 

I am able to create single record for the custom detail object using the VF page, how to define the number of records should get created on VF page itself, so that when i hit save button the defined number of records should get created??

 

my Apex Class:

public class OrderEntry 
{
    public List<RR__c> ords {get; set;}
    private final Opportunity parOrd;
    public OrderEntry(ApexPages.StandardController​ myController) {
        parOrd=(Opportunity)myController.getrecord​();
        ords = new List<RR__c>();
        RR__c LitOrd = new RR__c();
        LitOrd.Opportunity__c = parOrd.id;
        ords.add(LitOrd);}

    public void addrow() {
        RR__c LitOrd = new RR__c();
        LitOrd.Opportunity__c = parOrd.id;
        ords.add(LitOrd);}
            
    public void removerow(){
        Integer i = ords.size();
        ords.remove(i-1);}
            
    public PageReference save() {
         insert ords;
        PageReference parrec = new PageReference('​/'+ parOrd.id);
        parrec.setRedirect(true);
        return parrec; }
}

 

 

VF Page:

<apex:page standardController="Opportunity" extensions="OrderEntry">
    <apex:form >
    <apex:pageBlock title="Create Requisitions against Opportunities" >
                <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" rerender="error" />
            </apex:pageBlockButtons>
            <apex:pageBlockTable value="{!ords}" var="a" id="table">
                <apex:column headerValue="Opportunity Name">
                    <apex:inputField value="{!a.Opportunity__c}"/>
                </apex:column>               
               
            </apex:pageBlockTable>
    <apex:pageblockButtons location="bottom">
        <div style="text-align:right;margin-right:30px;font-wei​ght:bold;">
            <apex:commandLink value="Add Row" action="{!addRow}" rerender="table,error" immediate="true" />
&nbsp; | &nbsp;
<apex:commandLink value="Remove Row" action="{!removeRow}" rerender="table,error" immediate="true" />               

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

 

IspitaIspita

Do you want records with different intput values to be created? If yes then your page should have the ability and interface to accept multiple inputs?

If no that is you want multiple records to be created with same input in that case you can have a field which will take the input about the number of records to be created and after reading that value in your controller you can create the necessary number of clones.

 

Hope this helps.

NANCY1NANCY1

Hi Ispita,

 

Yes, I want multiple records to be created with same input..can you please provide me a sample code snippet for the same..in the contoller i have earlier posted....

 

thanks

Shashikant SharmaShashikant Sharma

Do you want to take multiple inputs for multiple records or clones of the record that you are creating now?

NANCY1NANCY1

I'll have a VF page with many input fields on it... with that i'll also have a Number of records input field on that page itself,

 

after entering the various input fields value, and entering the value for Number of records field, then on hitting the save button, i should have that number of records should get created...

 

I mean the input fields value will remain as it is for all the records that will be created....it will be cloning  kind of...

 

 

Shashikant SharmaShashikant Sharma

Change you controller to, I hope will help you

 

 public PageReference save() {
         List<RR__c> listOfOrds = new List<RR__c>();
        for(Integer i = 0 ; i < noofRecords ; i++)
         {      
         listOfOrds.add(ords);
          }
        insert listOfOrds;
        PageReference parrec = new PageReference('​/'+ parOrd.id);
        parrec.setRedirect(true);
        return parrec; }
}

 

NANCY1NANCY1

HI,

 

while doing this..i am getting the following error:

 

Error: NewRequisition Compile Error: Incompatible element type LIST<RR__c> for collection of SOBJECT:RR__c

 

public class NewRequisition
{
    public List<RR__c> rrdetail {get; set;}
    private Opportunity par_rrdetail;
   
    public NewRequisition(ApexPages.StandardController myController)
    {
        par_rrdetail=(Opportunity)myController.getrecord();
        rrdetail = new List<RR__c>();
        RR__c rrlist = new RR__c();
        rrlist.Opportunity__c = par_rrdetail.id;
        rrdetail.add(rrlist);
        }

    public void addrow()
    {
        RR__c rrlist = new RR__c();
        rrlist.Opportunity__c = par_rrdetail.id;
        rrdetail.add(rrlist);
    }
           
    public void removerow(){
        Integer i = rrdetail.size();
        rrdetail.remove(i-1);}
           
    public PageReference save()
    {
        List<RR__c> listOfOrds = new List<RR__c>();
        for(Integer i = 0 ; i < 2 ; i++)
         {     
         listOfOrds.add(rrdetail);
          }
        insert listOfOrds;

        //insert rrdetail;
        PageReference parrec = new PageReference('/'+ par_rrdetail.id);
        parrec.setRedirect(true);
        return parrec; }
       
        public PageReference cancel(){           
        PageReference home = new PageReference('/'+ par_rrdetail.id);       
        home.setRedirect(true);       
        return home;
          
    }  
}

Shashikant SharmaShashikant Sharma

listOfOrds.add(rrdetail);

 

in this statement you are adding a List in a List which is not allowed as listOfOrds takes RR__c not list of RR__c

 

instead you should do

          

 for(Integer i = 0 ; i < rrdetail.size(); i++)
         {     
         listOfOrds.add(rrdetail.get(i));

        }

 

you can nlso use, but do not use for loop if you do so



listOfOrds.addAll(rrdetail);

NANCY1NANCY1

Hi,

 

Visualforce Error

Help for this Page System.ListException: List index out of bounds: 1

Class.NewRequisition.save: line 32, column 25 External entry point

 

I have a question.. from where i am getting the rrdetail.size(); as i am creating a new record..

 

the error above i am getting when i have done something like that..

 

for(Integer i = 0 ; i < 2 ; i++)
         {     
         listOfOrds.add(rrdetail.get(i));
          }
        insert listOfOrds;

Shashikant SharmaShashikant Sharma

 use this instead , I used this in my last reply

 

for(Integer i = 0 ; i < rrdetail.size() ; i++)
         {     
         listOfOrds.add(rrdetail.get(i));
          }

       insert listOfOrds;

 

 

 

PLease let me know if any issue in it.

NANCY1NANCY1
Hi, This time i didn't get any error.. however please let me know how can pass the number of records i want to create, to the controller...Thanks
Shashikant SharmaShashikant Sharma

Do you want to take this from user or it is dependent on numer of some other records.

If you want to take it from user then have a property and use that n for loop

if dependent on no of some other record then use size of that list of records in for loop

 

NANCY1NANCY1

Hi,

 

Yes.. i want to take the number from the user..

 

Thanks

Shashikant SharmaShashikant Sharma

Create a Public Integer property

 

public Integer noOfRecordsToInsert {get;set}

 

In constructor initialize it to some default value if you want to.

 

Add a input text box on the VFP

 

<apex:inputText value={!noOfRecordsToInsert }" />

 

Your loop should be now

 

for(Integer i = 0 ; i < noOfRecordsToInsert ; i++)

{

}

sf.dev.ax1103sf.dev.ax1103

Hi All,

             I have a similar requirement but with different input values.Can you help me in sample code.

 

Thanks