Skip to content

Instantly share code, notes, and snippets.

@lukethacoder
Last active August 6, 2021 02:59
Show Gist options
  • Save lukethacoder/75fe4c1271f2b1cd6230c62e2ca1132b to your computer and use it in GitHub Desktop.
Save lukethacoder/75fe4c1271f2b1cd6230c62e2ca1132b to your computer and use it in GitHub Desktop.
Get an Object of sessionStorage data set by Salesforce Components. Should be able to work for localStorage too
function getSfSessionStorage() {
let sfSessionStorage = {};
// find the sessionStorage key that hold the SF data
Object.keys(sessionStorage).forEach((key) => {
if (key.includes('namespace')) {
sfSessionStorage = JSON.parse(sessionStorage[key]);
}
});
let sfSessionStorageData = {};
// loop over each sf object key
Object.keys(sfSessionStorage).forEach((sfKey) => {
// get the index of the sessionStorage from the sfSessionStorage object
const sessionStorageIndex = sfSessionStorage[sfKey];
// check the index exists
if (sessionStorage.hasOwnProperty(sessionStorageIndex)) {
// find the data with the sfKey
const item = sessionStorage[sessionStorageIndex]
// merge in the data + attempt to parse it
sfSessionStorageData = {
...sfSessionStorageData,
...{
[sfKey]: item && JSON.parse(item),
},
};
}
});
return sfSessionStorageData;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment