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 

using VF page..how to create userdefined number of records??

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-weight: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>
               

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Ah, so you aren't looking to fill in 5 records on the page, just a single record and then specify how many "instances" of this record to create when saving?

 

Something along the following lines should do it.  Not compiled or tested but should give you the basic idea.

 

Page:

 

<apex:page standardController="Opportunity" extensions="OrderEntry">
  <apex:form >
    <apex:pageBlock title="Create Requisitions against Opportunities" >
                <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" />
            </apex:pageBlockButtons>
            <apex:inputField value="{!ord.Opportunity__c}"/>
            <apex:inputField value="{!ord.Field1__c}"/>
            <apex:inputField value="{!ord.Field2__c}"/>
            <apex:inputField value="{!ord.Field3__c}"/>
            <apex:label value="Enter # records"/> <apex:inputText value="{!count}"/>
               
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

Controller:

 

public class OrderEntry 
{
    public RR__c ord {get; set}
    public String count {get; set;}
    private final Opportunity parOrd;

    public OrderEntry(ApexPages.StandardController myController) 
    {
        parOrd=(Opportunity)myController.getrecord();
        ords = new List<RR__c>();
        ord = new RR__c();
        ord.Opportunity__c = parOrd.id;
    }

    public PageReference save() 
    {
        Integer countInt=Integer.valueOf(count);
        List<RR__c> toInsert=new List<RR__c>();
        for (Integer idx=0; idx<countInt; idx++)
        {
           toInsert.add(ord.clone(false, true));
        }

        insert toInsert;
        PageReference parrec = new PageReference('/'+ parOrd.id);
        parrec.setRedirect(true);
        return parrec; 
    }
}

 

 

 

 

 

 

All Answers

Shashikant SharmaShashikant Sharma

Please answer below question

 

1) Do you want to save predefined no of opportunity record? 

2)Will you ask user to provide input for all records if more than one or you will create clone of one record?

 

bob_buzzardbob_buzzard

Your page/controller looks about right.  There's a couple of issues that might bite you:

 

(1) You have specified the immediate="true" attribute for your buttons - this will discard any changes that the user has made to the opportunity look field for the RR__c records.

(2) You are rerendering a component with an id of "error" for each of your buttons, but I can't see this component in the page.

 

What behaviour are you seeing?  I.e. are you receiving an error or are the records just not being created.

NANCY1NANCY1

Hi,

 

I am able to create a single record out of the post i have posted earlier...

 

The requirement is something like...on the VF itself... i need to enter the value for, number of records i want to create, with the inputs i have given on the VF itself for a particular opportunity...

 

it will be kind of cloning the records..for ex.

 

lets say i have 5 inputfields on the VF page includig the "headcount" i.e. number of records to be created inputfield (for example as 5).. then with all the inputs i have entered. 5 records should get created...

 

 

 

NANCY1NANCY1
any suggestion.. :) Thanks
bob_buzzardbob_buzzard

Ah, so you aren't looking to fill in 5 records on the page, just a single record and then specify how many "instances" of this record to create when saving?

 

Something along the following lines should do it.  Not compiled or tested but should give you the basic idea.

 

Page:

 

<apex:page standardController="Opportunity" extensions="OrderEntry">
  <apex:form >
    <apex:pageBlock title="Create Requisitions against Opportunities" >
                <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!save}" />
            </apex:pageBlockButtons>
            <apex:inputField value="{!ord.Opportunity__c}"/>
            <apex:inputField value="{!ord.Field1__c}"/>
            <apex:inputField value="{!ord.Field2__c}"/>
            <apex:inputField value="{!ord.Field3__c}"/>
            <apex:label value="Enter # records"/> <apex:inputText value="{!count}"/>
               
    </apex:pageBlock>
    </apex:form>
</apex:page>

 

Controller:

 

public class OrderEntry 
{
    public RR__c ord {get; set}
    public String count {get; set;}
    private final Opportunity parOrd;

    public OrderEntry(ApexPages.StandardController myController) 
    {
        parOrd=(Opportunity)myController.getrecord();
        ords = new List<RR__c>();
        ord = new RR__c();
        ord.Opportunity__c = parOrd.id;
    }

    public PageReference save() 
    {
        Integer countInt=Integer.valueOf(count);
        List<RR__c> toInsert=new List<RR__c>();
        for (Integer idx=0; idx<countInt; idx++)
        {
           toInsert.add(ord.clone(false, true));
        }

        insert toInsert;
        PageReference parrec = new PageReference('/'+ parOrd.id);
        parrec.setRedirect(true);
        return parrec; 
    }
}

 

 

 

 

 

 

This was selected as the best answer
NANCY1NANCY1

Hi,

 

while implementing the same..  i am getting the following error...

 

NewRequisition Compile Error: Method does not exist or incorrect signature: [LIST<RR__c>].clone(Boolean, Boolean)

 

 

 

Thanks

bob_buzzardbob_buzzard

That implies you have a list of records that you are trying to clone.  You shouldn't need to go that route if you are simply capturing a prototype record and cloning that a number of times.