Wednesday, 15 June 2011

Dynamic Binding : Use Action Functions on Field Set's Fields


Hi,

Earlier we have talked about how to use dynamic binding with or without field set and dynamic binding when object is also dynamic. Now its time to go one level ahead. 

My first question : Why we use dynamic binding ?
Ans : So that VFP Design can be configured on production org without any code change.

Mostly when we use field set or dynamic binding we can change our UI at any time but how to handle onclick event of any field if we have such requirement. We have only one inputField control in Repeat then how to call a javascript or actionFunction. We can not have dynamic UI at the cost of any business logic. So lets try to do it now


Requirement : We have a custom picklist field in the Field Set fields that we are displaying,  "Is Active" having value "Yes" , "No" , “None” , if user select s yes then “No Of Employee” fields should become mandatory and if he changes it to other then “Yes” then it should be non mandatory. And also set No Of Employe = 5 whenever "Is Active" fields changes its value.


Step 1: First I Created a Field Set On Account , With Five Fields in the Field Set






Step 2: Create the Controller Class like this

//Controller class
public class actionFunction_FieldSet {

    
    public Account account {get;set;}
    public boolean noOfEmpReq {get;set;}
    public String IsActive {get;set;}
    
    public actionFunction_FieldSet()
        {
            noOfEmpReq  = false;
            account = new Account();
        }
    //action function to be called
    public void actionFunctionM()
        {
            if(IsActive == 'Yes')
                noOfEmpReq = true;
            else
                noOfEmpReq = false;    
        }    
}

Step 3: Create a VFP like this
<apex:page controller="actionFunction_FieldSet" title="Field Set Use">
   <apex:form >
       <!-- Action function to determine required status for No of employee field -->
       <apex:actionFunction name="jsSetNoOfEmpReq" oncomplete="alert('action completed');" action="{!actionFunctionM}" immediate="true" reRender="pbsFS">
           <apex:param assignTo="{!IsActive}" value="IsActive" id="IsActive" name="noEmpR"/>
       </apex:actionFunction>
       <script>
           
           var numberofemployeesID;
           function setNoOfEmpReq(ctrlIsActive)
               {
                    
                    //Assign value to the no of employee field
                    if(document.getElementById(numberofemployeesID) != null)
                        document.getElementById(numberofemployeesID).value = 5;
                    //action function call 
                    jsSetNoOfEmpReq(ctrlIsActive.value);
               }
       </script>
       <apex:pageBlock title="Field Set Use">
       <apex:pageBlockSection title="Field Set Use" id="pbsFS">
           <apex:repeat value="{!$ObjectType.Account.FieldSets.DemoFieldSet}" id="ctrlRepeat" var="fieldAPIName">
               <!-- 
                    1. We have cretaed there input fields one will only if active__c field comes trough field set
                    2. second when numberofemployees comes
                    3. For other field
                    
                    Using such design we can have the dynamic design as well we can use onclick or onchange events on fields
                -->
               <apex:inputField value="{!account[fieldAPIName]}" onchange="setNoOfEmpReq(this);" rendered="{!IF(CONTAINS(LOWER(fieldAPIName), 'active__c') , true , false)}"/>
               <apex:inputField value="{!account[fieldAPIName]}" required="{!noOfEmpReq}" rendered="{!IF(CONTAINS(LOWER(fieldAPIName), 'numberofemployees') , true , false)}" id="NoOfEmp" />
               <apex:inputField value="{!account[fieldAPIName]}" rendered="{!IF(OR(CONTAINS(LOWER(fieldAPIName), 'active__c') , CONTAINS(LOWER(fieldAPIName), 'numberofemployees')) , false , true)}" /> 
               
               <script>
               /*
                   Set the id for numberofemployeesID field can be used in java script
               */
               if({!CONTAINS(LOWER(fieldAPIName), 'numberofemployees')})
                   {
                       numberofemployeesID = '{!$Component.ctrlRepeat}:NoOfEmp';
                   }
               </script>
           </apex:repeat>
       </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>>
</apex:page>

So that was a very basic requirement when we need to use action function on any event of field from field set. Using similar approach we can solve big problems as well. We can also achieved it using only a single inputField and using onclick and passing  field api name and performing actions only to valid fields otherwise return false.

<apex:page controller="actionFunction_FieldSet" title="Field Set Use">
   <apex:form >
       <!-- Action function to determine required status for No of employee field -->
       <apex:actionFunction name="jsSetNoOfEmpReq" oncomplete="alert('action completed');" action="{!actionFunctionM}" immediate="true" reRender="pbsFS">
           <apex:param assignTo="{!IsActive}" value="IsActive" id="IsActive" name="noEmpR"/>
       </apex:actionFunction>
       <script>
           
           var numberofemployeesID;
           function setNoOfEmpReq(ctrl , isctrlIsActive)
               {
                    if(isctrlIsActive)
                        {
                            //Assign value to the no of employee field
                            if(document.getElementById(numberofemployeesID) != null)
                                document.getElementById(numberofemployeesID).value = 5;
                            //action function call 
                            jsSetNoOfEmpReq(ctrl.value);
                        }
                    else
                        return false;    
               }
       </script>
       <apex:pageBlock title="Field Set Use">
       <apex:pageBlockSection title="Field Set Use" id="pbsFS">
           <apex:repeat value="{!$ObjectType.Account.FieldSets.DemoFieldSet}" id="ctrlRepeat" var="fieldAPIName">
                <!-- 
                    1. Used only one input field 
                -->
                
               <apex:inputField value="{!account[fieldAPIName]}" onchange="setNoOfEmpReq(this , {!CONTAINS(LOWER(fieldAPIName), 'active__c')});" required="{!IF(CONTAINS(LOWER(fieldAPIName), 'numberofemployees') , noOfEmpReq , false)}" id="fieldId" />
               
               
               <script>
               /*
                   Set the id for numberofemployeesID field can be used in java script
               */
               if({!CONTAINS(LOWER(fieldAPIName), 'numberofemployees')})
                   {
                       numberofemployeesID = '{!$Component.ctrlRepeat}:fieldId';
                   }
               </script>
           </apex:repeat>
       </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>>
</apex:page>


Other Related Posts : Dynamic Binding : When Object Name is Dynamic Also


         Regards
Shashikant Sharma




Thursday, 9 June 2011

Testing Apex : Structure of Test Class for Apex Class

I saw a lot of questions on Test Methods on developer community asking how to write test class for apex class. So here I am writing a basic structure of a test class which tests an Apex Class
@isTest
private class testApexClass{

    private static TestMethod void testClassMethod(){
        
        //Step 1 : Data Insertion
        //Insert all the data required for your trigger according to your logic written in the trigger
        
        //Try to give unique values in fields while creating test data, like Name = 'TestAccountName' etc
        
        //If any configuration setting is also needed in your trigger insert that as well
        //If you create any configuration setting then you will need to create a user also so that mixed dml exception do not occur
        
        //If class has a constructor with parameter Standard Controller, that means VFP is using this class as extention and has a standard controller then please create a standard controller prior to instance of class

        //Create instance of Class 
        MyClass cls = new MyClass();

        test.startTest();
        
        //call the method that you want to test using instance of class
        cls.MethodToTest()
        
        //assert your results using system.assert and system.asserEquals 
        
        test.stopTest();
        // IF you have used any static variables
        
        
    }


}

In above structure we have not talked about the advance problems like mixed dml exception and profile base testing. We leave it for other day :)

Regards

Wednesday, 8 June 2011

Testing Trigger : Structure of Test Class for Apex Trigger


Test methods are essential part of any application developed in salesforce. To prepare a managed package all test methods should run successfully and total code coverage has to be more than 75% . but we should not prepare test methods for coverage of code but we should prepare them to ensure functionality. We should prepare test methods for positive and negative cases both. Where positive case is when correct input is given and received expected results and negative cases where we provide negative input and any validation if we have should get fired.  Properly written test methods also helps us in regression testing as well.
Before we start structure of testmethods first l would like to mention some basic points :
  • 1.       Independent of Org Data : Test methods do not commit any data to database. We should always write test methods creating all required data in the test method itself. No data dependency should be on org data. If we have any configuration settings also then we should also set there values in the test method to make test method execution independent of org data.
  • 2.       Use test.startTest and test.stopTest , all data creation should be done prior to using test.startTest. Limits do get reset as any webservice invoke or dml action gets executed after test.startTest
  • 3.       Always test for bulk data also if your code executed on bulk data as well.
  • 4.       Always assert your result in test method against expected result this will ensure you that your code is giving expected results.

Now we will start structuring a trigger class for a trigger


@isTest
private class testClass_Trigger{

    private static TestMethod void testTrigger(){
        
        //Step 1 : Data Insertion
        //Insert all the data required for your trigger according to your logic written in the trigger
        
        //Try to give unique values in fields while creating test data, like Name = 'TestAccountName' etc
        
        //If any configuration setting is also needed in your trigger insert that as well
        //If you create any configuration setting then you will need to create a user also so that mixed dml exception do not occur
        
        test.startTest();
        
        //If there are any static variables which are used in the trigger please set there values
        
        //Perform the dml action on which trigger gets fired , like insert, update ,delete , undelete
        
        //assert your results using system.assert and system.asserEquals 
        
        test.stopTest();
        // IF you have used any static variables
        
        
    }


}
Using above structure we can write test methods for a structure.

Monday, 6 June 2011

Calculate Day of Date in Salesforce

In Apex Class :

Way 1 :
Calculate using DateTime Format : It is very interesting way of finding the Day of a date.
Date d = date.newinstance(1947,8, 15);
datetime myDate = datetime.newInstance(d.year(), d.month(), d.day());
String day = myDate.format('EEEE');


Way 2 : Prepared a class to find out day of given date using Mod of  7 to determine the day.

public class DayOfDate {
    Private Date startDate = date.newInstance(0001, 1, 1);
    Public Date selectedDate{get;set;}
    public String dayValue{get;set;}
    public Account acc {get;set;}
    public DayOfDate()
    {
        selectedDate = date.today();
        acc = new Account();
    }

    public void calculateDayOfDate()
    {
        List<String> listDay = new List<String>{'Saturday' , 'Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday'};
        selectedDate = acc.SLAExpirationDate__c;
        Integer remainder = Math.mod(startDate.daysBetween(selectedDate) , 7);
        dayValue = listDay.get(remainder);
    }



}


Visualforce Page :
<apex:page controller="DayOfDate">
  <apex:form>
  <apex:pageBlock>
      
      <apex:pageBlockSection columns="1">
          <apex:pageBlockSectionItem>
              <apex:outputLabel value="Enter Date"></apex:outputLabel>
              <apex:inputField value="{!acc.SLAExpirationDate__c}"/>
          </apex:pageBlockSectionItem>
          <apex:pageBlockSectionItem>
              <apex:outputLabel value="Day is : {!dayValue}"></apex:outputLabel>
          </apex:pageBlockSectionItem>
          <apex:commandButton action="{!calculateDayOfDate}" value="Find Day"/>
          
      </apex:pageBlockSection>
  </apex:pageBlock>
  </apex:form> 
</apex:page>

In above example I used Account objects date field  SLAExpirationDate__c to take input from user, you can take input using text field also, but in that case please format the date properly.


Calculate Day of a Date in formula field :

Create a formula field which should have return type 'Text'

IF(MOD(DateFieldAPIName__c - DATE(0001,1,1) , 7) == 0 , 'Saturday' , IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 1 , 'Sunday' , 
IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 2 , 'Monday' , 
IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 3 , 'Tuesday' , 
IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 4 , 'Wednesday' , 
IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 5 , 'Thursday' , 
IF(MOD(DateFieldAPIName__c- DATE(0001,1,1) , 7) == 6 , 'Friday' , 'No Day')))))) 
)

In this formula DateFieldAPIName__c is the field api name of the date field for which you want to know the day of date.


You can also Use "Case" instead of  IF in in above formula like this :

CASE(MOD(DateFieldAPIName__c - DATE(0001,1,1) , 7) , 0 , 'Saturday' , 1 , 'Sunday' ,  2 , 'Monday' , 3 , 'Tuesday' , 4 , 'Wednesday' , 5 , 'Thursday' , 6 , 'Friday'  , 'No Day Found')
Regards
Shashikant Sharma

Sunday, 5 June 2011

Dynamic Binding : When Object Name is Dynamic Also

Hi,

In earlier post we talked about dynamic binding with field set in this object name was static and fields were coming dynamically from Field Sets.Now we will talk about other way when both Object and Fields are dynamic.

Now we will take examples where object name and fields both are dynamic and fields are not coming from Field Sets either.

Controller Class
public class DynamicBinding {
    
    public sObject sObjectToBind {get;set;}
    public List<String> listObjectFields {get;set;}
        
    public DynamicBinding()
    {
        listObjectFields =  new List<String>();
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        //In this example I have hard coded the object name
        Schema.sObjectType sObjType = globalDescription.get('Account');
        
        sObjectToBind = sObjType.newSObject();
        Schema.DescribeSObjectResult r1 = sObjType.getDescribe();
        
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();
        Integer i = 0;
        for(Schema.SObjectField field : mapFieldList.values())
            {
                Schema.DescribeFieldResult fieldResult = field.getDescribe();
                if(fieldResult.isAccessible() && fieldResult.isUpdateable())
                    {
                        listObjectFields.add(fieldResult.getName());
                    }
                if(i == 5)
                    break;
                
                i++;
            }
                        
    }
}

Visulaforce Page

<apex:page controller="DynamicBinding">
  
   <apex:form>
       <apex:pageBlock title="Without Field Set Use">
       <apex:pageBlockSection title="Dynamic Object">
           <apex:repeat value="{!listObjectFields}" var="fieldAPIName">
               <apex:inputField value="{!sObjectToBind[fieldAPIName]}"/>
           </apex:repeat>
       </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>


</apex:page>


So in above example I used an sObject instance and showed 5 fields only of that Object type. In above example I used Account object you can decide any object pass the name from any parameter or get as input from User using Picklist.

Related Post : http://forceschool.blogspot.com/2011/06/dynamic-binding-using-field-sets.html

Regards




Age Calculation Formula

Hi,

I saw a question in developer community How to Calculate Age in formula field using Birth Date. It's reply was a very big formula

IF(MONTH(TODAY())> MONTH(DOB__c),YEAR(TODAY())-YEAR(DOB__c),IF(AND(MONTH(TODAY())= MONTH(DOB__c), DAY(TODAY())>= DAY(DOB__c)),YEAR(TODAY())-YEAR(DOB__c),(YEAR(TODAY())- YEAR(DOB__c))-1))

I Checked this this was working fine but It looked very complicated to me. So I thought of one formula that I used in first year of My Engg. in C Programming to determine age. In it we used to divide the date difference with 365.2425, why 365.2425 because we have leap years also so the avg. days in a year is not 365 but it is 365.2425 , So I created this new formula and it worked fine.

IF(ISNULL(DOB__c) , DOB__c , FLOOR((TODAY()-DOB__c +1)/365.2425))

I checked it, It is working fine in all cases , for leap year and all other cases that we usally check for age Calculation.

Regards

Decimal Rounding in an Apex

Hi,

As I was asked by my colleague how to round any decimal to certain decimal point, he was new to Salesforce. I looked in Math Methods in Apex Lang Ref. but could not find any as Round method there only returns floor or celling integer value removing all the decimal points. Then I suggested him to basic mathematic formula that my teacher told me in 10th standard
1)Multiply your number with nth power of 10 where n is the decimal point up to which we want to round it.
2) Use Math.Round then
3)Again divide it with n th power of 10

He was amazed that is that the way we do these small things in Apex, his comment made me to go again to Apex Lang Ref. Now I found a direct method in Decimal Method



Do this in System Logs :
Decimal decimalvalue = 23.55564543;
//rounds it up to 3 decimal places
d = d.setscale(3);
system.debug('decimalvalue : ' + d.scale());

Debug Result -> decimalvalue : 23.556

Decimal decimalvalue = 23.55534543;
//rounds it up to 3 decimal places
d = d.setscale();
system.debug('decimalvalue : ' + d.scale());

Debug Result -> decimalvalue : 23.555

So to my colleague and all please use this one and don't complicate things :)

Regards

Show Hierarchy in Visualforce Page

Hi,

I saw this question regularly in developer community how to show items in a hierarchy in VFP like this.
Parent
   Child 1
       Grand Child1
   Child 2
       Grand Child2

Now here is an Example :
I created a New Object Hierarchy
Added two fields in it :
1) AccountId : To save reference to Account Record
2)Parent Hierarchy : To save the Parent Hierarchy record

I created a Account : Test Hierarchy
And 5 Records for Hierarchy Object for this account record
 Parent1 : No parent hierarchy
 Child 1 : Parent Hierarchy =  Parent1
 Child 2 : Parent Hierarchy =  Parent1

 Grand Child 1 : Parent Hierarchy =  Child1
 Grand Child 2 : Parent Hierarchy =  Child2


Now our Aim is to show them in this way

Parent1
    Child1
          GrandChild11
    Child2
          GrandChild12

Apex Controller For this

//Create Hierarchy
public class hierarchy{

    //List of items to be displayed in hierarchy
    public List<hierarchyItem> hierarchyItemList {get;set;}
    
    //map of all  hierarchy records related to for a account for which we are showing hierarchy
    private Map<Id , Hierarchy__c> mapHierarchy = new Map<Id , Hierarchy__c>();
    
    //Hierarchy Index, used to determine spaces before the name starts
    private static Integer hirarchyIndex = 0;
    
    //name of account for which hierarchy is created
    public String AccountName {get;set;}
    
    public hierarchy()
    {
        if(ApexPages.currentPage().getParameters().get('accountid') != null)
        {
            ID AccountID = ApexPages.currentPage().getParameters().get('accountid');
           
            AccountName = [Select Name From Account where id =: AccountID].Name;
            //Get all hierarchy records related to account
            mapHierarchy = new Map<Id , Hierarchy__c>([Select Name , Parent_Hierarchy__c  From Hierarchy__c Where AccountId__c =: AccountID]);
            
           
            
            List<Hierarchy> listParentHierarchy = new List<Hierarchy>();
            hierarchyItemList = new List<hierarchyItem>();
            
            for(Hierarchy__c h : mapHierarchy.values())
                {
                    if(h.Parent_Hierarchy__c  == null)
                    {
                        hierarchyItemList.add(new hierarchyItem( h.Name , 0));
                        mapHierarchy.remove(h.id);
                        hierarchyItemList = getChildHierarchy(h.id , hierarchyItemList);
                    }
                    
                }
        }
    }

    //Method to get child records
    private List<hierarchyItem> getChildHierarchy(Id parentId , List<hierarchyItem> currentHierarchyItemList)
    {
        hirarchyIndex = hirarchyIndex + 1;
        for(Hierarchy__c h : mapHierarchy.values())
            {
                if(h.Parent_Hierarchy__c == parentId)
                    {
                        hierarchyItemList.add(new hierarchyItem( h.Name , hirarchyIndex));
                        mapHierarchy.remove(h.id);
                        //Get child records of child
                        hierarchyItemList = getChildHierarchy(h.id , hierarchyItemList);
                    }
                
            }
        hirarchyIndex = hirarchyIndex - 1;    
        return currentHierarchyItemList;    
    }    
    
    //Inner class for maintaing hierarchy items
    public class hierarchyItem{
        
        public String itemValue{get;set;} 
        public String hirarchyIndexNo{get;set;}
        public hierarchyItem(String itemValue , Integer hirarchyIndexNo)
            {
                String spaceCount = '';
                for(integer i = 0 ; i < hirarchyIndexNo ; i++)
                    spaceCount = spaceCount  + '&nbsp;&nbsp;';
                    
                this.itemValue = spaceCount + itemValue;
            }
    }

}


<apex:page controller="hierarchy">
  <apex:form>
     <apex:pageBlock>
         <apex:pageblockSection columns="1" title="Hierarchy For {!AccountName}">
             <apex:repeat value="{!hierarchyItemList}" var="item">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="{!item.itemValue}" escape="false"/>
                </apex:pageBlockSectionItem>
             </apex:repeat>
         </apex:pageblockSection>
     </apex:pageBlock>
  </apex:form>
 </apex:page>

In VFP I have used escape="false" in the outPutLabel to allow html rendering in our case &npsp; to show blank space. We can easily achieve this using it without it with use of Apex:Variable and calculate space with help of it.

Now give URL to your VFP in address bar and pass account id in query string
like this

/apex/YourPageName?accountid=00190000007EKOG

View The result



In above Child2 has come first as Hierarchy records are coming from SOQL are DESC order of Last Modified date.

Regards

Saturday, 4 June 2011

Dynamic Binding : Using Field Sets

Hi,

Dynamic binding has been an amazing feture released in Spring 11. Earlier in visualforce page means that the apex controls that used to be binded like {!sObjectAPIName.FieldAPIName} , where you have to provide object API Name and field API Name at compile time ( at the time you develope a page and save it), it was static binding. Now we can provide both of these at run time thats why it is dynamic binding.

Variations of Dynamic Binding :

1) Dynamic Field API Name : It can be sub divided in two parts
    A) Using Field Sets
    B) Without Using Field Sets where Field to be displayed are maintained in any List in apex class, we can save these list using any custom object in which we will save both object API Name and Field API Name.

2) Dynamic Object API Name and Field API Name both

We will start here by first

1) Dynamic Field API Name Using Field Set :

First we will create a field set on Account Object : Go to -> Customize -> Account -> Field Sets and create a new field set.

Here you will see three boxes for field selection
1)Account(This is Object Name) Object Fields : This box is only available on developer org. This is not visible on production org after managed packaged is installed there.
2)Available For the Field Set : These are the fields that are available to move to the "In the Field Set". This is available on production org after managed packaged is installed there.
3) In the Field Set : These are the fields that are actually rendered on Visualforce Page when Field Set is used.

Now I will add some fields see the below image


Now Save this field set.

Note : In summer 11 release layout for field set editor has been changed and made like page layout in the object. See this for more http://forceschool.blogspot.com/2011/05/summer-11-field-sets.html

Using field Set on Visulaforce Page

<apex:page standardController="Account" title="Field Set Use">
   <apex:form>
       <apex:pageBlock title="Field Set Use">
       <apex:pageBlockSection title="Field Set Use">
           <apex:repeat value="{!$ObjectType.Account.FieldSets.DemoFieldSet}" var="fieldAPIName">
               <apex:inputField value="{!Account[fieldAPIName]}"/>
           </apex:repeat>
       </apex:pageBlockSection>
       </apex:pageBlock>
   </apex:form>>
</apex:page>
In above example we used Account as standard controller, and used it for Field binding in Input Field. We can also use any account instance of account object from controller class also if we want. All fields of field set renders in order you save them in "In the Field Set" column. If any field is Hidden for any profile then field set will not render that field for that user.

You can also show field set of any related object which has a reference to parent object using relationship name.

Often it is asked whether we can create new fields and fields in the field set on production orgs. Answer to this is "Yes". As soon you create any field on the object on production org it comes in "Available Fileds" box and you can add them in the field set and render them on your VFP.

Related Post : http://forceschool.blogspot.com/2011/06/dynamic-binding-when-object-name-is.html

Thursday, 2 June 2011

Inner Wrapper Class in Apex Class

Hi,

I have seen a lot of questions regarding inner class in a apex class in developer community and issues that can be solved by use of inner class. So here I would like to show a problem which can also be solved by inner class.

Syntax of inner class :

public class myOuterClass {
   // Additional myOuterClass code here  
    
   class myInnerClass {
     // myInnerClass code here  
    
   }
}

Problem :
http://boards.developerforce.com/t5/General-Development/When-i-click-on-add-button-i-need-to-display-one-more-textbox/td-p/286261

When i click on add button i need to display one more textbox

Visual Force Page :
<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>


Apex 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;
                 }
         }
}

Here textBoxClass is the inner class

In above problem we used iner class now any new text box is binded with a new item in the list listvalueOfTextBox which is of type textBoxClass.

inner class acan be intancitiated like this
outerClass.innerclass instanceOfinnerClass = new outerClass.innerclass();

in above case it will be like
addTextrBox.textBoxClass instaceOftextBoxClass = new addTextrBox.textBoxClass('Test text Box');

Regards

Tweet