Monday, April 16, 2018

Salesforce Lightning: How to deal with cookies



You can use the below helper method to create a cookie.


So as an example, to create a cookie that stores the name of a visitor, you can call the helper method like below:
helper.createCookie('username', 'John Doe', 7);

To create a session cookie, don't pass any value for days. So you will be calling the helper method like below:
helper.createCookie('username', 'John Doe');

After executing the code above, the name of the cookie generated is LSKey[c]username. The name of the cookie is prefixed with LSKey[<c>] where c is the namespace. So to retrieve the value of this cookie, use the below code:


So as an example, to retrieve the cookie value, you can call the helper method like below:
helper.getCookie('username');

Check the link in the references section to check how to delete the cookie.

References: https://www.sitepoint.com/how-to-deal-with-cookies-in-javascript/
Share This:    Facebook Twitter

4 comments:

  1. Doesn't getCookie have to reference document.cookie in some way? It seems like something is missing.

    ReplyDelete
  2. Well thanks for this post, but it will be good if you provide with an example like I have a scenario where I have to track who visited the lightning component and how many times.

    Best Regards,
    Naveen Mosuru

    ReplyDelete
  3. how to we create the cookies without the prefix: LSKey[]?

    ReplyDelete
  4. If you disable Locker Service in session management settings to a older version of API, you can disable LSKEY while storing cookies from Lightning pages

    ReplyDelete

Total Pageviews

My Social Profiles

View Sonal's profile on LinkedIn

Tags

__proto__ $Browser Access Grants Accessor properties Admin Ajax AllowsCallouts Apex Apex Map Apex Sharing AssignmentRuleHeader AsyncApexJob Asynchronous Auth Provider AWS Callbacks Connected app constructor Cookie CPU Time CSP Trusted Sites CSS Custom settings CustomLabels Data properties Database.Batchable Database.BatchableContext Database.query Describe Result Destructuring Dynamic Apex Dynamic SOQL Einstein Analytics enqueueJob Enterprise Territory Management Enumeration escapeSingleQuotes featured Flows geolocation getGlobalDescribe getOrgDefaults() getPicklistValues getRecordTypeId() getRecordTypeInfosByName() getURLParameters Google Maps Governor Limits hasOwnProperty() Heap Heap Size IIFE Immediately Invoked Function Expression Interview questions isCustom() Javascript Javascript Array jsForce Lightning Lightning Components Lightning Events lightning-record-edit-form lightning:combobox lightning:icon lightning:input lightning:select LockerService Lookup LWC Manual Sharing Map Modal Module Pattern Named Credentials NodeJS OAuth Object.freeze() Object.keys() Object.preventExtensions() Object.seal() Organization Wide Defaults Override PDF Reader Performance performance.now() Permission Sets Picklist Platform events Popup Postman Primitive Types Profiles Promise propertyIsEnumerable() prototype Query Selectivity Queueable Record types Reference Types Regex Regular Expressions Relationships Rest API Rest Operator Revealing Module Pattern Role Hierarchy Salesforce Salesforce Security Schema.DescribeFieldResult Schema.DescribeSObjectResult Schema.PicklistEntry Schema.SObjectField Schema.SObjectType Security Service Components Shadow DOM Sharing Sharing Rules Singleton Slots SOAP API SOAP Web Services SOQL SOQL injection Spread Operator Star Rating stripInaccessible svg svgIcon Synchronous this Token Triggers uiObjectInfoApi Upload Files VSCode Web Services XHR
Scroll To Top