2

I have a situation where a user makes a change inside a Lightning page which uses web components. This page then sets parameters on to the URL and redirects to the same Lightning page.

The redirect code in the page is as follows -

window.location.replace(window.location.href + '?ObjectName=' + encodeURIComponent(this.listViewObject) 
                                             + '&ListViewName=' + encodeURIComponent(this.listViewName));

(the page is calling itself but with two additional parameters. This use-case might seem strange but I have good reasons!)

An example redirect URL looks as follows -

https://simpli-list-view-dev-dev-ed.lightning.force.com/lightning/n/simpli_lv__List_Views_Example_1?ObjectName=Account&ListViewName=PlatinumandGoldSLACustomers

As can be seen the parameters retrieved should be -

  • ObjectName - Account
  • ListViewName - PlatinumandGoldSLACustomers

I have looked around the web and it looks like I should be able to retrieve these parameters in the following way -

renderedCallback() {

    let params = new URL(window.location.href).searchParams;
            
    this.urlObject = params.get('ObjectName');
    this.urlListView = params.get('ListViewName');
}

From what I understand this needs to be done in the renderedCallback() method so that we have access to the main objects variables. i.e. urlObject and urlListView.

The redirect code looks to work fine. When I run the code I see the page refresh and I see the URL get updated with the additional parameters. But, when the renderedCallback() method is run the parameters are null.

What am I doing wrong?

Am I not calling the URL object or retrieving the params correctly?

I have noticed that soon after the redirect refreshes the page, the URL reverts back to the URL without the parameters. i.e.

https://simpli-list-view-dev-dev-ed.lightning.force.com/lightning/n/simpli_lv__List_Views_Example_1

Am I doing this in a totally incorrect way or am I just missing something?

1 Answer 1

3

You should try to stay away from the window object in LWC.

Locker intercepts calls to window and uses SecureWindow instead.

You can read more here.

In your lightning web component try using this

    import { LightningElement, wire } from 'lwc';
    import { CurrentPageReference } from 'lightning/navigation';
    
    export default class Sample extends LightningElement {
    
        @wire(CurrentPageReference)
        currentPageReference;
    
        connectedCallback() {
            console.log('ObjectName is: ' + this.currentPageReference.state.ObjectName );
            console.log('ListViewName is: ' + this.currentPageReference.state.ListViewName );
        }
    }

Source.

6
  • hmmm, this doesn't seem to be working for me. When I use the following - connectedCallback() { console.log( 'Param ' + this.currentPageReference.state.ObjectName ); } I get the following - "Param undefined"
    – Tom Ansley
    Commented Aug 20, 2020 at 21:58
  • Is ObjectName a query string parameter in the url of the page. What is the url of the page? I tested it again just now and it's working for me.
    – Arthlete
    Commented Aug 21, 2020 at 1:16
  • 1
    Yes, ObjectName is definitely a parameter on the URL - exactly as described above. I am wondering if somehow the parameters are getting scrubbed from the URL before it hits my page logic. By the time the page has finished loading the parameters can no longer be seen in the URL. It's like they magically disappear!
    – Tom Ansley
    Commented Aug 21, 2020 at 9:20
  • I think that this is the issue. Is there some sort of redirect that could be affecting the url you are setting originally?
    – Arthlete
    Commented Aug 21, 2020 at 15:43
  • 1
    Could it be the way I am setting the URL of the page that is being redirected to? I am also using the window object here too - window.location.replace(window.location.href + '?ObjectName=' + encodeURIComponent(this.listViewObject) + '&ListViewName=' + encodeURIComponent(this.listViewName)); Is there documentation on using SecureWindow? I have looked online and don't see any examples. Is this a different object that can be used (like window) or does it automatically get used?
    – Tom Ansley
    Commented Aug 21, 2020 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .