Search code examples
urlsalesforcesalesforce-lightninglwc

How to form an URL in LWC Community Page that can carry User Info?


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.


Solution

  • 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));
                }
            }
        }
    }