Tuesday 7 June 2011

Displaying fields of multiple objects on Vfpage and inserting data into objects

Friends, Today i have explored on wrapper class, i have one scenario that is ,i want to display fields from multiple objects on vfpage and inserting data into objects at a time.For this requirement i have explored for that finally i came to know that we can handle it using wrapper class. My first experience with wrapper class is superb,i got solution for my requirement. I am sharing my code i hope it will helpful for you.
        I have two objects Students, Lineitems
                Student object having fields  Name,StuNumber__c
                Lineitems  object having fields Name,lineItemNumber__c
Now my requirement is show fields from both objects on vfpage and inserting data into them.
for that i have designed wrapper class .

       public class wrapperclass
         {
             public student__c Std{get;set;}
           
             public lineItem__c litem{get;set;}
           
             public wrapperclass(Student__c Std,lineItem__c lit)
             {
                 this.std = Std;
                 this.litem = lit;
             }
         }

first we need to define objects in wrapper class as shown above.We can  add more number of objects also.
To access those fields on Vfpage ,we need to declare as follows,


 <apex:repeat value="{!lstobjfields}" var="item" rendered="{!IF(lstobjfields.size > 0 , true , false)}" >
           <apex:pageBlockSection >
               <apex:inputField value="{!item.Std.Name}"/>
               <apex:inputField value="{!item.Std.stunumber__c}"/>
               <apex:inputField value="{!item.litem.Name}"/>
               <apex:inputField value="{!item.litem.Lineitemnumber__c}"/>
           </apex:pageBlockSection>
       </apex:repeat>


 I am giving complete code , u can understand by comments,

Page:-
<apex:page controller="addTextrBox" tabStyle="student__c">
  <apex:Form >
      <apex:pageBlock >
       <apex:commandButton Value="Add" action="{!addfields}"/>
       <br></br>
        <apex:repeat value="{!lstobjfields}" var="item" rendered="{!IF(lstobjfields.size > 0 , true , false)}" >
           <apex:pageBlockSection >
               <apex:inputField value="{!item.Std.Name}"/>
               <apex:inputField value="{!item.Std.stunumber__c}"/>
               <apex:inputField value="{!item.litem.Name}"/>
               <apex:inputField value="{!item.litem.Lineitemnumber__c}"/>
           </apex:pageBlockSection>
       </apex:repeat>
        <apex:commandButton Value="save" action="{!saveText}"/>
       </apex:pageBlock>
   </apex:Form>
</apex:page>

Controller:-

public class addTextrBox
{
    public List<wrapperclass> lstobjfields
        {
          get;
          set;
        }  
    public addTextrBox ()
        {
            lstobjfields = new List<wrapperclass>();
        } 
    public PageReference addfields()
        {
            try
                {
                    lstobjfields.add(new wrapperclass(new Student__c(),new lineItem__c()));   // Adding fields toVfpage when user click on Add Button.
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }
     public class wrapperclass   // wrapper class to handle multiple objects.
         {
             public student__c Std{get;set;}
             public lineItem__c litem{get;set;}
             public wrapperclass(Student__c Std,lineItem__c lit)
             {
                 this.std = Std;
                 this.litem = lit;
             }
         }
      public PageReference saveText() // this method for inserting records into multiple objects.
        {
            try
                {
                    List<student__c> listStudent = new List<student__c>();
                    List<lineItem__c> lineItem = new List<lineItem__c>();
                   for(wrapperclass item : lstobjfields)
                    {
                            listStudent.add(item.Std);
                            lineItem.add(item.litem);  
                    }
                      if(listStudent.size() >  0 && lineItem.size() > 0)
                        insert lineItem;    // Inserting records in to multiple objects
                        insert listStudent;         
                }
            catch(Exception e)
                {
                    ApexPages.addMessages(e);
                }
            return ApexPages.currentPage();
        }
}







I hope this will be useful for u.

          
    

1 comment:

  1. Thanks Dude really helpful Stuff.keep up the good work

    ReplyDelete