function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
paul-lmipaul-lmi 

using a URL parameter to set a cookie

we're trying to use a URL parameter to set the page language, but to also set a cookie so that future loads of the same page, and other pages, will load in the proper language.

 

has anyone done this yet?  if so, care to share sample code?  we're running into an issue with what is likely order of operations, but I wanted to reach out and see if anyone's done this yet.

Ronen.ax767Ronen.ax767

Hi,

 

First get the parameters from the URL

 

PageReference pageRef = ApexPages.currentPage();
Map pageMap = pageRef.getParameters();
String MyParameterFromURL= pageMap.get('MyParam');

 



After that set up your cookie , and get cookies value with this functions:

 

   
public string CookieValue{get; set;}
   //public Cookie myCookies;     You can't do that!!! Cookie Class isn't Serializeable
    
   public Pagereference addToCookie(){
           
           InsertValueToCookie("New_Value_To_Cookie");
           return null;
   }

public void InsertValueToCookie(string val) { //create a new cookie with name 'MyFirstCookie',an initial value of Name at the page, // path 'null', maxAge '-1', and isSecure 'false'. Cookie myCookies=new Cookie('MyFirstCookie',val,null,-1,true); ApexPages.currentPage().setCookies(new Cookie[]{myCookies}); } public string getCookie() { Cookie myCookies = ApexPages.currentPage().getCookies().get('MyFirstCookie'); CookieValue=myCookies.getValue(); return CookieValue; }

 


    

 

hope this helps!