0

I am forming a web app in lightning Community Experience Builder using LWC, which already has an URL that carries the domain of my org.

Now I want to handover the URL to users along with an Id appended to its end, such that when the user visits the site I can retrieve the Id from URL using JS. example,

the original URL: cs-123.aig.lightning.force.com/form

User lands with: cs-123.aig.lightning.force.com/form?userId=123

I must be able to retrieve the userId when the component loads using renderedCallBack or connectedCallBack.

Thanks in advance.

Note:Lightning Navigation Service offered by LWC,doesnt work outside Salesforce,LEX.

1 Answers1

0

Plain JavaScript URLSearchParams should be enough. I have something similar on my project and works ok in community, with LWC embedded on the page.

connectedCallback() {
    // Try to read the preselected id from URL
    if (window.location.href) {
        try {
            let url = new URL(window.location.href);
            let id = url.searchParams.get('id');
            if (id) {
                this.userId = id;
            }
        } catch (e) {
            if (console) {
                console.log(JSON.stringify(e));
            }
        }
    }
}
eyescream
  • 18,088
  • 2
  • 34
  • 46
  • Thanks for the resposne. Right now, the URL doesn't involve any parameter. If I append a parameter in the URL Box and press Enter, it redirects me to the page and immediately wipes out the parameters. I had implemented the exact JS-based solution, which returns null/undefined! We must also note, that I am presently testing out, the component inside Salesforce, where it lives in the native LEX container. This may be wiping out any parameter that I append, as a consequence of server calls. – Dweepayan Sharma Nov 14 '21 at 18:16
  • It seems, that I had diagnosed the correct cause. Earlier my solution was supposed to work fine, but it didn't as I was testing it out within Salesforce Ligthning Exp and not the real community Page. Thank you so much, it was great to have you come down and answer my question! – Dweepayan Sharma Nov 14 '21 at 18:18
  • Glad I could help! Yes, communities are bit more "normal" with the navigation, in LEX you use navigation and sometimes hacks like `atob` & setting/reading the state from the part after hash... https://www.infallibletechie.com/2021/08/new-button-override-in-salesforce.html – eyescream Nov 14 '21 at 23:23