Thursday 19 January 2023

Get URL parameters in Lightning Components

 You can get lightning parameter in two way : 

1)  

lightning:isUrlAddressable Interface : Get URL parameters in Lightning Components


We can fetch URL param by using "{!v.PageReference.state.parameterName}" where parameterName is URL parameter name.


<aura:component  implements="lightning:isUrlAddressable,force:appHostable" access="global">
    <aura:attribute name="urlParam" type="string"/>
    <div class="slds-card">
    If you implements "lightning:isUrlAddressable" interface, then by 
        using v.pageReference,you can get URL params.
    <div class="slds-box">  
        Account Name is :  <b>{!v.pageReference.state.accname}</b>
        </div>
    </div> 
</aura:component>

"/lightning/n/tabName?accname=Naresh Thakur"

Output is : 
Account Name is : Naresh Thakur



2) You can write code in js component 
For Ex 

doInit : function(component, event, helper) {       
         var sPageURL = decodeURIComponent(window.location.search.substring(1));
         var sURLVariables = sPageURL.split('&');
         var sParameterName;
         for (var i = 0; i < sURLVariables.length; i++) {
             sParameterName = sURLVariables[i].split('=');             
             if (sParameterName[0] === 'conId') {
                 component.set('v.tempConId',sParameterName[1]);
             }
         }
         
        // Other code goes here
    },

Call it 
https://YourURL/Pagename?conId = aio000000000ADSuiq