Retrieve list of cookie in Salesforce -
i have shopping cart feature in salesforce page. i've created cookiejar class store cookie based on tutorial retrieve list of cookie. cart page retrieve 1 cookie contains 1 product. retrieve cookies i've stored. how do that?
apex controller:
public pagereference addtocart() { for(displayproducts p : products) { if(0 < p.qtytobuy) { //create instance of cookiejar class, passing values entered in fields cookiejar c = new cookiejar(p.productid, p.name, string.valueof(p.qtytobuy)); } } pagereference pageref = new pagereference('/apex/cart'); pageref.setredirect(true); return pageref; } public string getcartcontents() { string msg = '<ul>\n'; cookie thecookie; thecookie = apexpages.currentpage().getcookies().get('productid'); if(thecookie != null) msg +=thecookie.getvalue(); thecookie = apexpages.currentpage().getcookies().get('productname'); if(thecookie != null) msg +=thecookie.getvalue(); thecookie = apexpages.currentpage().getcookies().get('qtytobuy'); if(thecookie != null) msg += thecookie.getvalue(); } public class cookiejar { public cookiejar(string productid, string productname, string qtytobuy) { cookie pid = new cookie('productid', productid,null,315569260,false); cookie pname = new cookie('productname', productname,null,315569260,false); cookie qty = new cookie('qtytobuy', qtytobuy,null,315569260,false); //set page cookies using setcookies() method apexpages.currentpage().setcookies(new cookie[]{pid, pname, qty}); } }//end cookiejar inner class }
vf cart page:
<apex:form > <apex:pageblock title="your cart" id="shopping_cart"> <apex:outputtext value="{!cartcontents}" escape="false"/> </apex:pageblock> </apex:form>
you have code require in question:
apexpages.currentpage().getcookies()
pagereference.getcookies() return map keys cookie.
map<string, system.cookie[]> cookiemap = for(string cookiekey : cookiemap.keyset()) { system.debug('cookie key: ' + cookiekey + ' value ' + cookiemap.get(cookiekey).getvalue()); }
incidentally, salesforce stackexchange site great place ask salesforce specific questions.
Comments
Post a Comment