Last active
August 6, 2021 02:59
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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