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
KrisGopiKrisGopi 

How to Develope a MultiAdd Page without using JavaScript

Hai ,buddies i have problem with code ,that we add a add,Save buttons to a page and display the customFields any of 3 fields ex-Name,Departement from Employee and  if we click on  add  then add the new row and same as all that means i have to show the page as a Multi Add Page and above all added pages we given enter the values the all will be save in the Custom object and allso we delete the  a perticular row tha we selected using a 'action button ' .

 

 :manhappy:

Best Answer chosen by Admin (Salesforce Developers) 
SPDSPD

ok..

 

i had tried a similar example before....what i did was that i created a inner class that contained a refernce to the object (the one in which the record should get added)...here's a code of my example hope this helps...

 

 

/* Controller */

 

 

 

public class SaveMultipleRecords

{
   
    //will hold the account records to be saved
    public List<Account>lstAcct  = new List<Account>();
   
    //list of the inner class
    public List<innerClass> lstInner
    {    get;set;    }
   
    //will indicate the row to be deleted
    public String selectedRowIndex
    {get;set;}   
   
    //no. of rows added/records in the inner class list
    public Integer count = 1;
    //{get;set;}
   
   
    ////save the records by adding the elements in the inner class list to lstAcct,return to the same page
    public PageReference Save()
    {
        PageReference pr = new PageReference('/apex/MultiAdd');
       
        for(Integer j = 0;j<lstInner.size();j++)
        {
            lstAcct.add(lstInner[j].acct);
        }
        insert lstAcct;
        pr.setRedirect(True);
        return pr;
    }
       
    //add one more row
    public void Add()
    {   
        count = count+1;
        addMore();       
    }
   
    /*Begin addMore*/
    public void addMore()
    {
        //call to the inner class constructor
        innerClass objInnerClass = new innerClass(count);
       
        //add the record to the inner class list
        lstInner.add(objInnerClass);   
        system.debug('lstInner---->'+lstInner);           
    }/* end addMore*/
   
    /* begin delete */
    public void Del()
    {
        system.debug('selected row index---->'+selectedRowIndex);
        lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
        count = count - 1;
       
    }/*End del*/
   
   
   
    /*Constructor*/
    public SaveMultipleRecords(ApexPages.StandardController ctlr)
    {
   
        lstInner = new List<innerClass>();
        addMore();
        selectedRowIndex = '0';
       
    }/*End Constructor*/
       


    /*Inner Class*/
    public class innerClass
    {       
        /*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
        public String recCount
        {get;set;}
       
       
        public Account acct
        {get;set;}
       
        /*Inner Class Constructor*/
        public innerClass(Integer intCount)
        {
            recCount = String.valueOf(intCount);       
           
            /*create a new account*/
            acct = new Account();
        }/*End Inner class Constructor*/   
    }/*End inner Class*/
}/*End Class*/

 

 

 

and the visualforce Page

 

 

<apex:page StandardController="Account" extensions="SaveMultipleRecords" id="thePage">
<apex:form >
<apex:pageblock id="pb" >
    <apex:pageBlockButtons >
        <apex:commandbutton value="Add" action="{!Add}" rerender="pb1"/>
        <apex:commandbutton value="Save" action="{!Save}"/>
    </apex:pageBlockButtons>
   
       
        <apex:pageblock id="pb1">
                    
           
        <apex:repeat value="{!lstInner}" var="e1" id="therepeat">
                <apex:panelGrid columns="4">
               
                <apex:panelGrid>
                    <apex:facet name="header">Del</apex:facet>
                    <apex:commandButton value="X" action="{!Del}" rerender="pb1">
                        <apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param>
                    </apex:commandButton>
                </apex:panelGrid>   
               
                <apex:panelGrid >
                    <apex:facet name="header">Country</apex:facet>
                    <apex:inputfield value="{!e1.acct.ShippingCountry}"/>
                </apex:panelGrid>
               
                <apex:panelGrid >
                    <apex:facet name="header">IsActive</apex:facet>
                    <apex:inputfield value="{!e1.acct.Active__c}"/>
                </apex:panelGrid>
               
                <apex:panelGrid >
                    <apex:facet name="header">Name</apex:facet>
                    <apex:inputfield value="{!e1.acct.Name}"/>
                </apex:panelGrid>
        </apex:panelgrid>
        </apex:repeat>
    </apex:pageBlock>
       
</apex:pageblock>
</apex:form>
</apex:page>

All Answers

SPDSPD
i guess you are referring to creating a page that has a add and a save button in it..wen you click on add button a new row should get added on the same page while retaining the previous values entered by you..and whenever you click on save the record should get saved..right?
KrisGopiKrisGopi

yes...if u have any example..then please post it....

 

thanks in advance..:manhappy:

SPDSPD

ok..

 

i had tried a similar example before....what i did was that i created a inner class that contained a refernce to the object (the one in which the record should get added)...here's a code of my example hope this helps...

 

 

/* Controller */

 

 

 

public class SaveMultipleRecords

{
   
    //will hold the account records to be saved
    public List<Account>lstAcct  = new List<Account>();
   
    //list of the inner class
    public List<innerClass> lstInner
    {    get;set;    }
   
    //will indicate the row to be deleted
    public String selectedRowIndex
    {get;set;}   
   
    //no. of rows added/records in the inner class list
    public Integer count = 1;
    //{get;set;}
   
   
    ////save the records by adding the elements in the inner class list to lstAcct,return to the same page
    public PageReference Save()
    {
        PageReference pr = new PageReference('/apex/MultiAdd');
       
        for(Integer j = 0;j<lstInner.size();j++)
        {
            lstAcct.add(lstInner[j].acct);
        }
        insert lstAcct;
        pr.setRedirect(True);
        return pr;
    }
       
    //add one more row
    public void Add()
    {   
        count = count+1;
        addMore();       
    }
   
    /*Begin addMore*/
    public void addMore()
    {
        //call to the inner class constructor
        innerClass objInnerClass = new innerClass(count);
       
        //add the record to the inner class list
        lstInner.add(objInnerClass);   
        system.debug('lstInner---->'+lstInner);           
    }/* end addMore*/
   
    /* begin delete */
    public void Del()
    {
        system.debug('selected row index---->'+selectedRowIndex);
        lstInner.remove(Integer.valueOf(selectedRowIndex)-1);
        count = count - 1;
       
    }/*End del*/
   
   
   
    /*Constructor*/
    public SaveMultipleRecords(ApexPages.StandardController ctlr)
    {
   
        lstInner = new List<innerClass>();
        addMore();
        selectedRowIndex = '0';
       
    }/*End Constructor*/
       


    /*Inner Class*/
    public class innerClass
    {       
        /*recCount acts as a index for a row. This will be helpful to identify the row to be deleted */
        public String recCount
        {get;set;}
       
       
        public Account acct
        {get;set;}
       
        /*Inner Class Constructor*/
        public innerClass(Integer intCount)
        {
            recCount = String.valueOf(intCount);       
           
            /*create a new account*/
            acct = new Account();
        }/*End Inner class Constructor*/   
    }/*End inner Class*/
}/*End Class*/

 

 

 

and the visualforce Page

 

 

<apex:page StandardController="Account" extensions="SaveMultipleRecords" id="thePage">
<apex:form >
<apex:pageblock id="pb" >
    <apex:pageBlockButtons >
        <apex:commandbutton value="Add" action="{!Add}" rerender="pb1"/>
        <apex:commandbutton value="Save" action="{!Save}"/>
    </apex:pageBlockButtons>
   
       
        <apex:pageblock id="pb1">
                    
           
        <apex:repeat value="{!lstInner}" var="e1" id="therepeat">
                <apex:panelGrid columns="4">
               
                <apex:panelGrid>
                    <apex:facet name="header">Del</apex:facet>
                    <apex:commandButton value="X" action="{!Del}" rerender="pb1">
                        <apex:param name="rowToBeDeleted" value="{!e1.recCount}" assignTo="{!selectedRowIndex}"></apex:param>
                    </apex:commandButton>
                </apex:panelGrid>   
               
                <apex:panelGrid >
                    <apex:facet name="header">Country</apex:facet>
                    <apex:inputfield value="{!e1.acct.ShippingCountry}"/>
                </apex:panelGrid>
               
                <apex:panelGrid >
                    <apex:facet name="header">IsActive</apex:facet>
                    <apex:inputfield value="{!e1.acct.Active__c}"/>
                </apex:panelGrid>
               
                <apex:panelGrid >
                    <apex:facet name="header">Name</apex:facet>
                    <apex:inputfield value="{!e1.acct.Name}"/>
                </apex:panelGrid>
        </apex:panelgrid>
        </apex:repeat>
    </apex:pageBlock>
       
</apex:pageblock>
</apex:form>
</apex:page>

This was selected as the best answer
KrisGopiKrisGopi
hey thanks very much...this was a great help...:smileymad::smileysurprised::smileyvery-happy:...thats m situation now...initially i was very frustrated...now u made me happy...thanks again