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

    Using Cookie Class of APEX

    We all are familiar with the word cookie. Cookies are small pieces of data that any website stores on your computer in order to pass that data between pages. Likewise APEX also gives us the ability to use cookies, however APEX cookies are little bit different than the normal cookies that we get in the browser. For APEX the cookie is a class that we can use to set data in a Pagereference variable so that it can be used later on. This is what I will demonstrate, how to use cookie class of APEX.

    The default cookie class gives us multiple functions to use for accessing cookies like:

    • getName() – This function returns the name of the cookie that the class has.
    • getValue() – This function provides the value of the cookie string.
    • isSecure() – Returns true if the cookie can only be accessed by HTTPS, else false.

    In the above class we have a simple pagereference method in which we have created a cookie object and to the constructor we have passed various arguments. The first argument is the name of the cookie, that we will use to access the cookie later on. Second argument is the actual value which will be passed into the cookie. The third variable is the path, which specifies where the cookie will be stored. Setting the path to null will just store the cookie in root. Next argument specifies the lifespan of the cookie. To set a normal lifespan pass any positive value to the cookie. Setting the value to 0 will destroy the cookie. A negative value will make the cookie a session cookie and will end with the session. The last argument specifies whether the cookie can only be retrieved by secure method, i.e. HTTPS or not.

    After creating the cookie we have to set it in the pagereference variable by the setcookies method. This method takes a list of cookie type as input so we have passed the object as a list.

    VF page Code

    The Vf page code is very much general and is used to just call the function setData() to set cookies.

    The cookie data is fetched by the pagereference function getCookies(), which returns a map of all the stored cookies. From that map we get the cookie by the general get() function of the map and then using the getValue() function of the cookie we fetch the cookie value stored.

    Once we get it we can show it on the VF page. We can even show it on any page within the org by simply using the above command in apex controller. The cookie will be available for the provided lifespan.


    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 cookie class of APEX, 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