function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandeep SahrawatSandeep Sahrawat 

Pass URL Parameters to a Lightning Component inside a community.

1. I have used Salesforce Napili template to create a new Community. 
2. I have created a new page in this template where I am exposing a new Lightning Component I created. 
3. The lightning component accepts email and phone as two attributes. 
4. These attributes should be passed to the lightning component embedded in the community as URL parameters. I am not able to do this today. 
 
I would like to pass pass parameters to the lightning component inside community using URL parameter like: 
http://<<communityURL>>/<<communityName>>?&email=rama@test.org&phone=999-999-9999 
 
The email and phone parameters from the community URL should be passed to the component included in the community. 
 
I know a way of doing this through lightning applications. I have seen this link: 
http://salesforce.stackexchange.com/questions/106543/lightning-app-getting-url-parameter 
 
But, my usecase is different. I don't have a lightning application. I only have a lightning component embedded in a Salesforce Lightning Community.
 
Please suggest, if this can be done.
Best Answer chosen by Sandeep Sahrawat
Damon ShawDamon Shaw
Hi Sandeep,

I've been working on replacing the standard App Launcher componet in the community builder and wanted to get a url param to define the way the page is displayed, header and css etc.
 
I was able to add a function that gets the url params when the component loads and then set it to an attribute, here's a basic version of what worked for me.

Controller.js
({
    doInit : function(component, event) {

        var action = component.get("c.findApps");
        action.setCallback(this, function(a) {



            // the function that reads the url parameters
            var getUrlParameter = function getUrlParameter(sParam) {
                var sPageURL = decodeURIComponent(window.location.search.substring(1)),
                    sURLVariables = sPageURL.split('&'),
                    sParameterName,
                    i;

                for (i = 0; i < sURLVariables.length; i++) {
                    sParameterName = sURLVariables[i].split('=');

                    if (sParameterName[0] === sParam) {
                        return sParameterName[1] === undefined ? true : sParameterName[1];
                    }
                }
            };

            //set the src param value to my src attribute
            component.set("v.src", getUrlParameter('src'));



        	var settings = a.getReturnValue();            
        });
        $A.enqueueAction(action);       
    }
})
Component.cmp
<aura:component controller="CustomAppLauncherController" implements="forceCommunity:availableForAllPageTypes" >

    <aura:attribute name="src" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    {!v.src}

</aura:component>

hope this helps

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi Sandeep Sahrawat,

As far as I know, we can Pass Data From One Component to Another component by creating a lightning event.

Please refer below link for example.

https://www.forcetalks.com/blog/how-to-pass-data-from-one-component-to-another-in-salesforce-lightning/
 
and the other way as you said is lightning applications.

http://ofthecloud.blogspot.sg/2015/12/passing-url-parameters-to-controller-in.html
 
Hope this helps you!

Please mark it as solved if this helps you so that it will make available for other as a proper solution.

Thanks and Regards
Sandhya
Damon ShawDamon Shaw
Hi Sandeep,

I've been working on replacing the standard App Launcher componet in the community builder and wanted to get a url param to define the way the page is displayed, header and css etc.
 
I was able to add a function that gets the url params when the component loads and then set it to an attribute, here's a basic version of what worked for me.

Controller.js
({
    doInit : function(component, event) {

        var action = component.get("c.findApps");
        action.setCallback(this, function(a) {



            // the function that reads the url parameters
            var getUrlParameter = function getUrlParameter(sParam) {
                var sPageURL = decodeURIComponent(window.location.search.substring(1)),
                    sURLVariables = sPageURL.split('&'),
                    sParameterName,
                    i;

                for (i = 0; i < sURLVariables.length; i++) {
                    sParameterName = sURLVariables[i].split('=');

                    if (sParameterName[0] === sParam) {
                        return sParameterName[1] === undefined ? true : sParameterName[1];
                    }
                }
            };

            //set the src param value to my src attribute
            component.set("v.src", getUrlParameter('src'));



        	var settings = a.getReturnValue();            
        });
        $A.enqueueAction(action);       
    }
})
Component.cmp
<aura:component controller="CustomAppLauncherController" implements="forceCommunity:availableForAllPageTypes" >

    <aura:attribute name="src" type="String"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />

    {!v.src}

</aura:component>

hope this helps
This was selected as the best answer
Sandeep SahrawatSandeep Sahrawat
Thank you Sandhya and Damon for your response

Damon, I will try this on the weekend. Hopefully this should work..

Thanks
Sandeep Sahrawat
PrabhaPrabha
@Sandeep Sahrawat, 

I have updated my post with another and simpler solution, you want to know another technique , 
http://ofthecloud.blogspot.sg/2015/12/passing-url-parameters-to-controller-in.html , see it.

I am using design properties to pass the values.

HTH
Prabhan;
john s.john s.

Hello 

I*m a real visual force rookie 

Im not sure if this the right approach for me what you're have discussed here.

I would like to close the gap between Enterprise and Professional related to Dashboard Logged In User Feature  in Lightning.

Is there any way to solve this with an Visual Force page in Lightning with the Professional Edition  ?

I know that i can solve this really easy in classic with passing through the url ... but not in lightning 

so  where i have to begin ?

Any help is highly appreciated ...

thanks in advance 

best regards john 



 

Pasan   EeriyagamaPasan Eeriyagama
Is there a Salesforce provided method yet to achieve this? Thanks for sharing this method anyway, better to have this idea (https://success.salesforce.com/ideaView?id=0873A000000TrLwQAK) delivered. 
Prasanta TrailheadPrasanta Trailhead
Salesforce navigation from lightning component to lightning community page. It is truely possible. The detail step is given here :

https://salesforceblog.tumblr.com/post/178882204863/salesforce-navigation-from-lightning-component-to
 
Aaron Webster 21Aaron Webster 21
I know the op talked about LWC, but I think this will work for that as well. For those who are still looking for a way to do this. I have a salesforce Experience site that has a VisualForce page on it. The VisualForce page is dependent on URL parameters. This is how I retrieved the URL parameter for the site.
 
var url = (window.location != window.parent.location)
  ? document.referrer
  : document.location.href;
console.log({url});
const queryString = url.split('?')[1];
console.log({queryString});
const urlParams = new URLSearchParams(queryString);
console.log({urlParams});
const sfAccountId = urlParams.get("accountId");
console.log({sfAccountId});