23

We are planning to convert some of our VF pages to Lightning apps. Some our VF pages, particularly surveys are URL parameter-driven, since we send survey links directly to users, i.e. https://[vf_instance].force.com/apex/CaseSurvey?caseid=xxxxx

Say we convert the VF page to an Lightning app, we would probably be sending the Lightning survey like this https://[vf_instance].lightning.force.com/c/CaseSurvey.app?caseid=xxxxx

Problem:

In VF page controller, PageReference.getParameters() contains the much needed caseid

However, in Lightning component controller, PageReference.getParameters() does not contain any URL parameters.

How can I get the URL parameter in Lightning app/component apex controller?

THANKS!

3 Answers 3

32

In lightning whatever attribute you define can be passed as a query parameter .

Lets take a look with sample example

<aura:application>
  <aura:attribute name="whom" type="String" default="world"/>
   Hello {!v.whom}!
</aura:application>

Here is how the result will look like

enter image description here

Now I will add a query parameter with value as per my attribute defined whom in the URL and now the result will be as below

enter image description here

In very simple terms you just need to define an aura:attribute and you are good

 <aura:attribute name="caseid" type="String"/>
6
  • 1
    Good explanation @Mohith . Just an addition, it only supports String type of attributes :) Jan 25, 2016 at 6:47
  • 4
    This will only work is the attribute is defined in the .app. What is I want to pass the parameter to a nested component?
    – Allen Mann
    Apr 15, 2016 at 13:19
  • 2
    Does it alos work with embedded components in custom lightning page in COmmunity?
    – SEuser
    Sep 19, 2016 at 10:39
  • 2
    Thanks @AllenMann for tip that the <aura:attribute> must be defined in the app containing your component. To pass the parameter to any nested components then set them as attribute values on the nested components like <c:YourCmp someAttribute="{!v.whom}"/> where your containing app declares <aura:attribute name="whom" type="String"/> per @Mohith's example
    – Doug Ayers
    Nov 21, 2016 at 21:39
  • salesforce.stackexchange.com/questions/149445/… .Here is an example how you pass and go down the chain Nov 21, 2016 at 22:10
8

Just adding this answer for completion of the thread with the latest release. After Summer 18 release (API version 43 and up) we can utilise lightning:isUrlAddressable interface.

Implement lightning:isUrlAddressable interface and use pageReference attribute.

Example. - Component Assume url is https://<instance>.lightning.force.com/lightning/cmp/<namespace>__componentName?testAttribute=XYZ

<aura:component implements="lightning:isUrlAddressable">
   <aura:handler name="init" value="{!this}" action="{!c.doInit}" description="Handler for valueInit event fired when the component has been initialised"/>
        {!v.pageReference.state.testAttribute}
</aura:component>

Component Controller

({
    doInit : function(component, event, helper) {
        console.log(component.get("v.pageReference").state.testAttribute);
    }
})

Console output will look like: "XYZ"

1
  • 2
    That works for a Lightning Component, but I couldn't get it to work for a Lightning Application Dec 28, 2018 at 18:08
2

There isn't any documented way of doing that, but I was able to do so using a hack.

The Salesforce Lightning URL is something like "/one/one.app#_______" . The blank contains some encoded value in the URL. The encoded value is nothing but the details of the Component/Page in Base64 encoded form.

Use the below code in Javascript in Visualforce to navigate to Lightning Components-

<script>
var compDefinition = { 
"componentDef" : "c:NewContact", 
"attributes" :{ 
      "recordTypeId" : "012xxxxxxxxxxxx",
      "testAttribute" : "attribute value"
 } 
};
// Base64 encode the compDefinition JS object
var encodedCompDef = btoa(JSON.stringify(compDefinition));
window.parent.location = "/one/one.app#"+encodedCompDef;
</script>

You must log in to answer this question.

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