Menu Close
    Hire us Request quote Reading list Switch to dark mode

    How to write a wrapper class in APEX

    Any Salesforce developer would be familiar with creating parent child relationship queries in APEX. For example getting a list of products with their respective prices we use such queries. However when we take that data to Visualforce pages it becomes a little bit hectic to show that data. This problem can be solved with the help of wrapper class. This is what I am going to show you today, how to create a wrapper class in apex.

    APEX code

    For this example we will be making a small module to mass update product active status. As we all know products have an isActive boolean value which tells whether the product could be added to a pricebook or not. The controller code for this module is as follows:

    In the above example I have created an inner-class at the bottom of the code which is the most important part of this controller. That class holds a boolean variable and an object of product2 type. The use of this variable will be explained later on in the code. For now, lets focus on the other parts. We have a list of productwrapper type and this list is being iterated over to check or uncheck the isActive field. One thing to notice here is that I have added all the record object to a list of product2 type. That is because DML does not work on Wrapper class and you have to take out the object to perform a DML operation on that object.

    Visualforce Code

    The code for the visualforce page is as follows:

    In this page we have used the wrapper class list to show the data in the data table. Here we have used the selected boolean value of the wrapper class. The value is now mapped to the checkbox. When the checkbox will be selected this value will become active and it will be reflected back in the controller. The rest is just basic visualforce code.


    The output of my code will be something like this:

    . . .
    Subscribe Articles

    Just enter your email below to subscribe and receive weekly updates about the most interesting similar articles.

    You need to accept conditions to receive letters.
    You've Subscribed!

    Share article via

    . . .


    That’s all about using wrapper class, for any further queries feel free to add a ticket at:

    Or let us know your views on how to make this code better, in comments section below.

    . . .


    Add Your Comment

    Be the first to comment.

    Back to Top
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again

    Table of Content

    Hide Index