+ Start a Discussion
vibrationvibration 

When i click on add button i need to display one more textbox(contoller also)

Hi, Good Morning. can any one help me to solve the following......... I have an object invoice__c. It has only one field name.i would like to add the records into object using VF page. Initiallay, In VF page I have Add button,Save button & a text box. When i click on add button i need to display one more textbox. Like this i can add any number of textboxes. But when i entered data into these textboxes and clicked on save. I would like to add all the names into object in the form of records. That means, each name will be added into object as a record. i need controller class also Can any one help me.

Best Answer chosen by Admin (Salesforce Developers) 
vibrationvibration

 

class
public class addTextrBox 
{
    public List<textBoxClass> listValueOfTextBox
        { 
          get; 
          set; 
        }    
    public addTextrBox ()
        {
            listvalueOfTextBox = new List<textBoxClass>();
        }
    public PageReference addTextBox() 
        {
            try
                {
                    listvalueOfTextBox.add(new textBoxClass('Serial Number' + (listvalueOfTextBox.size() +  1)));
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }
     public class textBoxClass
         {
             public string textBoxLabel{get;set;}
             public string textBoxValue{get;set;}
             
             public textBoxClass(String textBoxLabel)
                 {
                     this.textBoxLabel = textBoxLabel;
                 }
         }
      public PageReference saveText() 
        {
            try
                {
                    List<student__c> listStudent = new List<student__c>();
                    for(textBoxClass item : listvalueOfTextBox)
                        {
                            student__c studentObj = new student__c(stunumber__c = Decimal.valueOf(item.textBoxValue));
                        
                            listStudent.add(studentObj);
                        }
                    
                    if(listStudent.size() >  0)
                        insert listStudent;    
                         
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }
}

 

 

vfpage:

 

<apex:page controller="addTextrBox" tabStyle="student__c">
  <apex:Form >
       <apex:commandButton Value="Add Serial Number" action="{!addTextBox}"/>
       <br></br>
        
       <apex:repeat value="{!listvalueOfTextBox}" var="item" rendered="{!IF(listvalueOfTextBox.size > 0 , true , false)}" >
           <apex:outPutLabel value="{!item.textBoxLabel}">
           </apex:outPutLabel>
           <apex:inputText value="{!item.textBoxValue}"/>
           <br></br>
       </apex:repeat>
   </apex:Form>
</apex:page>

 

error:Error: Compile Error: Incompatible element type addTextrBox.textBoxClass for collection of SOBJECT:student__c at line 45 column 29


 

All Answers

Shashikant SharmaShashikant Sharma

Controller class

 

public class addTextrBox 
{

    public List<textBoxClass> listValueOfTextBox
        { 
          get; 
          set; 
        }    
    public addTextrBox ()
        {
            listvalueOfTextBox = new List<textBoxClass>();
        }
    public PageReference addTextBox() 
        {
            try
                {
                    listvalueOfTextBox.add(new textBoxClass('TextBox' + (listvalueOfTextBox.size() +  1)));
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }

     public class textBoxClass
         {
             public string textBoxLabel{get;set;}
             public string textBoxValue{get;set;}
             
             public textBoxClass(String textBoxLabel)
                 {
                     this.textBoxLabel = textBoxLabel;
                 }
         }
}

 

 

VFP

 

 

<apex:page controller="addTextrBox">
  <apex:Form>
       <apex:commandButton Value="Add Text Box" action="{!addTextBox}"/>
       <br></br>
       <apex:repeat value="{!listvalueOfTextBox}" var="item" rendered="{!IF(listvalueOfTextBox.size > 0 , true , false)}" >
           <apex:outPutLabel value="{!item.textBoxLabel}">
           </apex:outPutLabel>
           <apex:inputText value="{!item.textBoxValue}"/>
           <br></br>
       </apex:repeat>
   </apex:Form>
</apex:page>

 Use above I think your page is ready for adding text boxes

 

Please update styling for and layout arrangement as per your need.

 

 

vibrationvibration
Thanks, please add the code in save concept also...
Shashikant SharmaShashikant Sharma

What is your object and field api name in which you want to save it

vibrationvibration

1)object name = lineItem

 

 

2)Fields :

           a) LineItemId(auto generated)

           b) Lineitemnumber (textbox value)

 

output

 

    LineItemId    Lineitemnumber

    10001            12000

    10002            65378

vibrationvibration

i am waiting for ur reply . very urgent

Shashikant SharmaShashikant Sharma

Save Code

 

public PageReference saveText() 
        {
            try
                {
                    List<LineItem> listLineItem = new List<LineItem>();
                    for(textBoxClass item : listvalueOfTextBox)
                        {
                            LineItem lineItemObj = new LineItem(Lineitemnumber = item.textBoxValue);
                            listLineItem.add(item);
                        }
                    
                    if(listLineItem.size() >  0)
                        insert listLineItem;    
                         
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }

 

 

vibrationvibration

 Compile Error: Invalid initial expression type for field student__c.stunumber__c, expecting: Decimal at line 44 column 82

Shashikant SharmaShashikant Sharma

Use : Decimal.valueof(Your Fields API Name) like this

LineItem lineItemObj = new LineItem(Lineitemnumber = Decimal.valueOf(item.textBoxValue));

 

 

Before you insert or update the record.

vibrationvibration

Error: Compile Error: Incompatible element type addTextrBox.textBoxClass for collection of SOBJECT:student__c 

vibrationvibration

 

class
public class addTextrBox 
{
    public List<textBoxClass> listValueOfTextBox
        { 
          get; 
          set; 
        }    
    public addTextrBox ()
        {
            listvalueOfTextBox = new List<textBoxClass>();
        }
    public PageReference addTextBox() 
        {
            try
                {
                    listvalueOfTextBox.add(new textBoxClass('Serial Number' + (listvalueOfTextBox.size() +  1)));
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }
     public class textBoxClass
         {
             public string textBoxLabel{get;set;}
             public string textBoxValue{get;set;}
             
             public textBoxClass(String textBoxLabel)
                 {
                     this.textBoxLabel = textBoxLabel;
                 }
         }
      public PageReference saveText() 
        {
            try
                {
                    List<student__c> listStudent = new List<student__c>();
                    for(textBoxClass item : listvalueOfTextBox)
                        {
                            student__c studentObj = new student__c(stunumber__c = Decimal.valueOf(item.textBoxValue));
                        
                            listStudent.add(studentObj);
                        }
                    
                    if(listStudent.size() >  0)
                        insert listStudent;    
                         
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }
}

 

 

vfpage:

 

<apex:page controller="addTextrBox" tabStyle="student__c">
  <apex:Form >
       <apex:commandButton Value="Add Serial Number" action="{!addTextBox}"/>
       <br></br>
        
       <apex:repeat value="{!listvalueOfTextBox}" var="item" rendered="{!IF(listvalueOfTextBox.size > 0 , true , false)}" >
           <apex:outPutLabel value="{!item.textBoxLabel}">
           </apex:outPutLabel>
           <apex:inputText value="{!item.textBoxValue}"/>
           <br></br>
       </apex:repeat>
   </apex:Form>
</apex:page>

 

error:Error: Compile Error: Incompatible element type addTextrBox.textBoxClass for collection of SOBJECT:student__c at line 45 column 29


 

This was selected as the best answer
vibrationvibration

Hi,

 Very urgent plz correct my code.

 

i add two text box, when i click a button,

 

textbox1:12345

textbox2:34567

 

when i click save button , 

 

o/p:

 

serialid   serialnumber

 s1001     12345

 s1002     34567

 

 

vibrationvibration

thank u very much it works fine. plz send ur mail ID.

 

I am azarudeen from chennai. past 10 months i m working salesforce. 

Shashikant SharmaShashikant Sharma

Hi,

 

Don't you think my answer deserved to be marked as solution. I spent 1 full hour to write that code.

vibrationvibration

Hey Extremly sorry sharma. now i iaccept your solution dnt mistake me.

 

your code :

for(textBoxClass item : listvalueOfTextBox)
                        {
                            LineItem lineItemObj = new LineItem(Lineitemnumber = item.textBoxValue);
                            listLineItem.add(item);
                        }
                    
                    if(listLineItem.size() >  0)
                        insert listLineItem;    

Change code:

 listLineItem.add(lineitemobj);

 

Shashikant SharmaShashikant Sharma

No issues  :)

vibrationvibration
hi,
 thanks for ur reply.
 Now output is displayed only serial number. i want to add a another lookup field in VF page. how to add? 
i think solution is standard controller. but if i add standard controller i cant add textboxes.
please tell asap
 
class:
public  class MultipleSerialSave
{
   
    
    public List<textBoxClass> listValueOfTextBox
        { 
          get; 
          set; 
        }    
   
    public MultipleSerialSave ()
        {
            listvalueOfTextBox = new List<textBoxClass>();
            
        }
    public PageReference addTextBox() 
        {
            try
                {
                    listvalueOfTextBox.add(new textBoxClass('Serial Number' + (listvalueOfTextBox.size() +  1)));
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }
     public class textBoxClass
         {
             public string textBoxLabel{get;set;}
             public string textBoxValue{get;set;}
             
             public textBoxClass(String textBoxLabel)
                 {
                     this.textBoxLabel = textBoxLabel;
                 }
         }
        
   
      public PageReference saveText() 
        {
            try
                {
            
                    List<Serial_Number__c> listSerialNumber = new List<Serial_Number__c>();
                   
                    for(textBoxClass item : listvalueOfTextBox)
                        {
                           Serial_Number__c SerialNumberObj = new Serial_Number__c(Serial_No__c = item.textBoxValue);
                           listSerialNumber.add(SerialNumberObj);
                        }
                    
                    if(listSerialNumber.size() >  0)
                        insert listSerialNumber;    
                         
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }
}
vfpage:
<apex:page Controller="MultipleSerialSave" tabstyle="Serial_Number__c">
  <apex:Form >
      
       <apex:commandButton Value="Add Serial Number" action="{!addTextBox}"/>
       <apex:commandButton action="{!saveText}" value="save"/>
       <br></br>
        
       <apex:repeat value="{!listvalueOfTextBox}" var="item" rendered="{!IF(listvalueOfTextBox.size > 0 , true , false)}" >
           <apex:outPutLabel value="{!item.textBoxLabel}">
           </apex:outPutLabel>
           <apex:inputText value="{!item.textBoxValue}"/>
           <br></br>
       </apex:repeat>
   </apex:Form>
</apex:page>
Shashikant SharmaShashikant Sharma

Do yo know which lookup you need to display or it is dynamic?

vibrationvibration

display.

 

o/p

 

productname = lookupfield

 

serialnumber1 = textbox

 

serialnumber2 = textbox

 

click save

vibrationvibration

i m waiting for ur reply

Shashikant SharmaShashikant Sharma

I am not getting your req. correctly

1) If you know what lookup you want then just create instance of the object that has the lookup and use input field and bind that lookup field and render it conditionaly

 

But again I will ask is your lookup is dynamic if it is then you will have to do it differently.

vibrationvibration

warehouse product object:

field = warehouse product name

 

serial number object

fields:

 warehouse product name(master details of warehouse product)

 serial number

 

o/p:

 

warehouse product name  =  lookup value

serial number 1-10001

serial number 2-10002

 

wahen i click save , all the value stored in serial number object. two serial number created.

 

 

Shashikant SharmaShashikant Sharma

You can use

 

Create an instance of warehouse product object in the class , like

 

Public warehouseproductName__c whpObj {

                                            get

                                                 {

                                                     if(whpObj == null)

                                                    {

                                                              whpObj  = new warehouseproductName__c (); 

                                                     }

                                           }

                                          set;

                                          }

 

In vfp add this

<apex:inputField value="{!whpObj.LookupFieldName__c}" />

vibrationvibration

i add following code only wareproductname save, serial number not saved

 

 

Public warehouseproductName__c whpObj {

                                            get

                                                 {

                                                     if(whpObj == null)

                                                    {

                                                              whpObj  = new warehouseproductName__c (); 

                                                     }

                                           }

                                          set;

                                          }

 

In vfp add this

<apex:inputField value="{!whpObj.LookupFieldName__c}" />

vibrationvibration

 

This is my code . only insert warehouseproductname. i want to insert serialnumber and warehouseproductname.

 

 

Mycode:

 

 

class:

 

 

public  class MultipleSerialSave
{
   
    Serial_Number__c Serial;
   public Serial_Number__c getSerial() {
         
      if(Serial == null) Serial = new Serial_Number__c();
      return Serial;
     
  }
    public List<textBoxClass> listValueOfTextBox
        { 
          get; 
          set; 
        } 
        
  
   
 
    public MultipleSerialSave ()
        {
            listvalueOfTextBox = new List<textBoxClass>();
            
        }
    public PageReference addTextBox() 
        {
            try
                {
                    listvalueOfTextBox.add(new textBoxClass('Serial Number' + (listvalueOfTextBox.size() +  1)));
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }
     public class textBoxClass
         {  
            
             public string textBoxLabel{get;set;}
             public string textBoxValue{get;set;}
             
             public textBoxClass(String textBoxLabel)
                 {
                     this.textBoxLabel = textBoxLabel;
                 }
           
         }
        
   
      public PageReference saveText() 
        {
          insert serial;
          
            try
                {
                   
                    List<Serial_Number__c> listSerialNumber = new List<Serial_Number__c>();
                   
                    for(textBoxClass item : listvalueOfTextBox)
                        {
                           Serial_Number__c SerialNumberObj = new Serial_Number__c(Serial_No__c = item.textBoxValue);
                           listSerialNumber.add(SerialNumberObj);
                        }
                    
                    if(listSerialNumber.size() >  0)
                      
                        update listSerialNumber; 
                          
                         
                }
               
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }
}
page:
<apex:page Controller="MultipleSerialSave" tabstyle="Serial_Number__c">

  <apex:Form >
       <apex:inputField value="{!Serial.Warehouse_Product_Name__c}"/> 
       <apex:commandButton Value="Add Serial Number" action="{!addTextBox}"/>
       
       <br></br>
        
       <apex:repeat value="{!listvalueOfTextBox}" var="item" rendered="{!IF(listvalueOfTextBox.size > 0 , true , false)}" >
           
           <apex:outPutLabel value="{!item.textBoxLabel}">
           </apex:outPutLabel>
           <apex:inputText value="{!item.textBoxValue}"/>
           <br></br>
       </apex:repeat>
       <apex:commandButton action="{!saveText}" value="save"/>
   </apex:Form>
</apex:page>

 

vibrationvibration

Hi sharma. thanks for last 3 days help. i got a solution. 

 

thank u very muck man

 

i m asking ur mail id.

vibrationvibration

How to Remove serial Number in same code?

 

when i click remove serial number button, one textbox is removed

viswsanathviswsanath

HI vibration,

Please give me replay if u got solution for removing text box.