Read Query Parameter in Lightning Component

Before Summer 18, to read parameter in Lightning Component, we had to wrap Lightning Component in Lightning Application. After Summer 18, it has changed for good.

In this blog post we would create a formula field to launch Lightning Component (yes, you read it right). And then we would read URL parameter in Lightning Component using pageReference provided by interface lightning:isUrlAddressable.

Create a formula field on Account to launch Lightning Component and read its parameter.

Formula Field to Launch Lightning Component
Formula Field to Launch Lightning Component

As you can see in above image, we would be using Hyperlink method to create link in Lightning Experience

HYPERLINK("/lightning/cmp/c__ReadParameter?accountName="+ Name , "Launch")

Create a Lightning Component with below source code

After clicking on formula field, Lightning component would look like below image showing capability to read URL parameter

Read URL Parameter in Salesforce Lightning Component
Read URL Parameter in Salesforce Lightning Component

Posted

in

by

Comments

19 responses to “Read Query Parameter in Lightning Component”

  1. Chait Avatar
    Chait

    Thanks so much Jitendra, really useful article.
    Will the formula field work in Salesforce Classic and salesforce 1 mobile too?
    Chait

    1. Jitendra Avatar

      It will work only in Lightning Experience as you cannot launch Lightning component directly in classic.

      1. Vinod Batra Avatar
        Vinod Batra

        Hi Jitendra, I have used it many times but after my sandbox updated to summer’19 it is not working please check my question.
        Thanks in Advance !!
        https://developer.salesforce.com/forums/ForumsMain?id=9062I000000IGRMQA4

  2. Sreenivas Avatar
    Sreenivas

    You are the guy…who is tired less learner.

  3. Anshul Jain Avatar
    Anshul Jain

    Is it possible to open this page as a popup, instead of redirecting it to a new screen?

    1. Harsha BR Avatar
      Harsha BR

      Hi Anshul,

      Did you find how to open a pop up in lightning instead of opening a new window in formula field?

      Thanks & Regards,
      Harsha BR

      1. Gautam Avatar
        Gautam

        Hi Harsha,

        Were you able to figure out something to open a pop up instead of a new ta?

  4. Amrika Avatar
    Amrika

    I am getting the below error-

    This page isn’t available in Salesforce Lightning Experience or mobile app.

  5. […] Using the new Page Reference mechanic to pass query parameters from URLs into Lightning Components […]

  6. Ghanshyam Avatar
    Ghanshyam

    It does not work. When you click on the hyperlink of the formula, the page loses the parameter and its reference data.

  7. Vishal Agrawal Avatar
    Vishal Agrawal

    will it work in community if yes i am having trouble passing it in my url as i am getting null when doing console.log

    will it work in community pages, if yes then how, can you brief me?

    this is url and i am passing parameter in it as key= sunil https://deb1-coro.cs17.force.com/v2/s/invdetail?key=sunil

    var pageReference = component.get(‘v.pageReference’); console.log(JSON.stringify(pageReference));

    but getting null as log

    1. Madhusudhan Avatar
      Madhusudhan

      Is it working in lighting community?

  8. Maanas Desai Avatar
    Maanas Desai

    This is not working anymore after Summer 19 . YOU Need to append namespace c__ to all parameters which you are including in url and same in the compoenent. For example above formula field would be

    HYPERLINK(“/lightning/cmp/c__ReadParameter?c__accountName=”+ Name , “Launch”)

    & code
    ({
    init : function(component, event, helper) {
    var pageReference = component.get(“v.pageReference”);
    component.set(“v.accountName”, pageReference.state.c__accountName);
    }
    })

    1. Tulasiram Avatar
      Tulasiram

      Manas Desai you made my day. Thanks a lot.

  9. Nilesh Avatar
    Nilesh

    Manas Desai GREAT JOB. Thanks a lot.

  10. Nitish Yadav Avatar
    Nitish Yadav

    Hello Jitendra Sir !!! This won’t work if my lightning component is on Record Page or on any flexiPage ??

  11. Madhusudhan Avatar
    Madhusudhan

    Will this work in community?

  12. Dave Roberts Avatar

    Manas Desai : Brilliant!!!

  13. Harshit Singh Avatar
    Harshit Singh

    Hello Sir,

    As mentioned above, the formula link for your Aura Component named ReadParameter.cmp is:
    HYPERLINK(“/lightning/cmp/c__ReadParameter?accountName=”+ Name , “Launch”)

    I wonder what would be the formula link for LWC named readParameter.html?

    I have tried HYPERLINK(“/lightning/cmp/c__readParameter?accountName=”+ Name , “Launch”), but it is not working.

    Could you (or anyone reading this) please help me here?

    Thanks in advance!

Leave a Reply to Dave Roberts Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.