I have a class that gets the params from the URL, then if the system already have a cookie it retrieves their value. if the system doesnt have a cookie it creates a cookie and assign a value.

Then just before the methods ends it check if the value is a valid value, if it is not it should destroy the cookie. For some reason I am not able to achieve this last part.

the below code is an what i currently have:

public static string getcm() {
    string cmParam = ApexPages.currentPage().getParameters().get('cm');
    Cookie cm = ApexPages.currentPage().getCookies().get('cm');
    string cmId;
    if(cm == null){
        if(cmParam != null){
            cm = new Cookie('cm',cmParam,null,31536000,false);
            ApexPages.currentPage().setCookies(new Cookie[]{cm});  
            cmId = cm.getValue();
        }
    }else{
        cmId = cm.getValue();            
    }
    try{
        cmId = [SELECT Id FROM Account WHERE Client_ID__c =:cmId].Id ;
    } catch(QueryException qe){
        cmId = null;
        //delete the cookie as it has invalid data
        cm = new Cookie('cm',null,null,0,false);
    }
    return cmId;
}  

I have also tried adding a question like

if (cmId == null){
    cm = new Cookie('cm',null,null,0,false);
}

but no success

  • Are you sure that your query cmId = [SELECT Id FROM Account WHERE Client_ID__c =:cmId].Id doesn't return any records, 100%? By specifying the 4th argument (maxAge) to 0 in your constructor the cookie should get deleted. Code looks fine, double check that your query fails and your catch block is executed. – Boris Bachovski Aug 14 '14 at 0:35
  • 1
    Thanks Bachovski, I was missing setit up back again the cookie, ApexPages.currentPage().setCookies(new Cookie[]{cm}); I have post my answer hopefuly it helps to anyone doing something similar – manza Aug 18 '14 at 4:09
up vote 1 down vote accepted

I figure it out what was going on. In the catch section I need it to setup again the cookie:

ApexPages.currentPage().setCookies(new Cookie[]{cm}); 

so the actual code looks like:

public static string getcm() {
    string cmParam = ApexPages.currentPage().getParameters().get('cm');
    Cookie cm = ApexPages.currentPage().getCookies().get('cm');
    string cmId;
    if(cm == null){
        if(cmParam != null){
            cm = new Cookie('cm',cmParam,null,31536000,false);
            ApexPages.currentPage().setCookies(new Cookie[]{cm});  
            cmId = cm.getValue();
        }
    }else{
        cmId = cm.getValue();            
    }
    try{
        cmId = [SELECT Id FROM Account WHERE Client_ID__c =:cmId].Id ;
    } catch(QueryException qe){
        cmId = null;
        //delete the cookie as it has invalid data
        cm = new Cookie('cm',null,null,0,false);
        ApexPages.currentPage().setCookies(new Cookie[]{cm}); 
    }
    return cmId;
}  

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.