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
Narayana Reddy 30Narayana Reddy 30 

If cookie is not present, set the cookie value

Hi,
I have a requirement to check whether specific cookie is present or not and if cookie is not present, based on URL parameters, create the cookie and set the cookie . Cookie value need to be passed to common utility class --> method to store the cookie data in Salesforce object . Trying with below code . Kindly check and let me know if any suggestions . After reading the cookie value, is there any need to pass the data in hidden parameters to forms ?  (OR) Since cookie is already there in browser session, read the cookie value in main controller and pass the value to common utility class --> method ?

Cookie anaCookie = ApexPages.currentPage().getCookies().get('anaCookie '); 



ana_param1 = apexpages.currentpage().getparameters().get('ana_param1');
        ana_param2 = apexpages.currentpage().getparameters().get('ana_param2');
        ana_param3 = apexpages.currentpage().getparameters().get('ana_param3');

if (anaCookie == null && (ana_param1  != null || ana_param2  != null || ana_param3  != null )) {

 String anaCookieValue  = 'ana_param1='+ana_param1+'&'+'ana_param2='+ana_param2+'&'+'ana_param3='+ana_param3
                      anaCookie = new Cookie('anaCookie ',anaCookieValue,'.salesforce-sites.com',-1,true);
            ApexPages.currentPage().setCookies(new System.Cookie[]{anaCookie});
} else if (anaCookie != null) {
            anaCookieValue = anaCookie.getValue();
            List<string> anadataLiStr = anaCookieValue.split('&');
            for (string anadatast: anadataLiStr ){
              if (anadatast.contains('ana_param1'))
                  ana_param1= anadatast.substringAfter('=');
              else if (anadatast.contains('ana_param2'))
                  ana_param2 = anadatast.substringAfter('=');
              else if (anadatast.contains('ana_param3'))
                  ana_param3 = anadatast.substringAfter('=');
               }
    utilityclass.recordUpdate(anaCookieValue);
}

Utility class
=======
public utilityclass {

recordUpdate(Sting anaCookieValue) {
// Parse the cookie value as 3 parameters
// Assign three values to three parameters in object record
// Update the record
}
 

}



 
SubratSubrat (Salesforce Developers) 
Hello ,

Can you please try with the below corrected code :
String ana_param1 = ApexPages.currentPage().getParameters().get('ana_param1');
String ana_param2 = ApexPages.currentPage().getParameters().get('ana_param2');
String ana_param3 = ApexPages.currentPage().getParameters().get('ana_param3');

Cookie anaCookie = ApexPages.currentPage().getCookies().get('anaCookie');

if (anaCookie == null && (ana_param1 != null || ana_param2 != null || ana_param3 != null)) {
    String anaCookieValue = 'ana_param1=' + ana_param1 + '&' + 'ana_param2=' + ana_param2 + '&' + 'ana_param3=' + ana_param3;
    anaCookie = new Cookie('anaCookie', anaCookieValue, '.salesforce-sites.com', -1, true);
    ApexPages.currentPage().setCookies(new Cookie[]{anaCookie});
} else if (anaCookie != null) {
    String anaCookieValue = anaCookie.getValue();
    List<String> anadataLiStr = anaCookieValue.split('&');
    for (String anadatast : anadataLiStr) {
        if (anadatast.contains('ana_param1'))
            ana_param1 = anadatast.substringAfter('=');
        else if (anadatast.contains('ana_param2'))
            ana_param2 = anadatast.substringAfter('=');
        else if (anadatast.contains('ana_param3'))
            ana_param3 = anadatast.substringAfter('=');
    }
    UtilityClass.recordUpdate(ana_param1, ana_param2, ana_param3);
}


Suggestions:

In your code, you have a typo where you have extra spaces in the cookie name 'anaCookie '. It should be 'anaCookie' without the extra space.

It's a good practice to declare and initialize variables before using them. In your code, ana_param1, ana_param2, and ana_param3 were used before declaration. I have moved the declarations to the beginning.

Since you're passing the cookie values to the UtilityClass.recordUpdate method, there is no need to pass them as hidden parameters in forms. You can directly use the updated values.

I noticed a missing semicolon at the end of the anaCookieValue line. I have added it in the updated code.

If this helps , please mark this as Best Answer.
Thank you.