How to Get Parent Id when overriding standard actions with a Lightning Component in salesforce1 app ?


In this blog I will explain how you can get Parent Id when overriding standard actions with a lightning component.

For example I have changed my Opportunity New button action with a custom Lightning Component. 

Let's create a new account and from the related tab try creating the new Opportunity. Have you noticed any change in URL ?

Let me share the URL with you : 
_________________________________________________________________________
https://kapilbatra1-dev-ed.lightning.force.com/lightning/o/Opportunity/new?recordTypeId=0127F000000htlpQAA&additionalParams=accid%3D0017F00002S6g9H%26&inContextOfRef=1.eyJ0eXBlIjoic3RhbmRhcmRfX3JlY29yZFBhZ2UiLCJhdHRyaWJ1dGVzIjp7Im9iamVjdEFwaU5hbWUiOiJBY2NvdW50IiwicmVjb3JkSWQiOiIwMDE3RjAwMDAyUzZnOUhRQVIiLCJhY3Rpb25OYW1lIjoidmlldyJ9LCJzdGF0ZSI6e319&count=3
_________________________________________________________________________

In above URL you can notice an "additionalParams" parameter containing the Parent ID.  But in the Ssalesforce1 App the scenario is completely different. In Salesforce1 App you have to perform some additional steps.

Let's check the URL first in the Salesforce1 App

_________________________________________________________________________
https://kapilbatra1-dev-ed.lightning.force.com/lightning/o/Opportunity/new?recordTypeId=0127F000000htlp&inContextOfRef=1.eyJ0eXBlIjoic3RhbmRhcmRfX2RpcmVjdENtcFJlZmVyZW5jZSIsImF0dHJpYnV0ZXMiOnsibmFtZSI6Im9uZTphbG9oYVBhZ2UiLCJhdHRyaWJ1dGVzIjp7ImFkZHJlc3MiOiIvc2V0dXAvdWkvcmVjb3JkdHlwZXNlbGVjdC5qc3A%2FZW50PU9wcG9ydHVuaXR5JnNhdmVfbmV3X3VybD0lMkYwMDYlMkZlJTNGYWNjaWQlM0QwMDE3RjAwMDAyUzZnOUgmbmF2aWdhdGlvbkxvY2F0aW9uPVJFTEFURURfTElTVCJ9fSwic3RhdGUiOnt9fQ%3D%3D
_________________________________________________________________________

In above URL you will notice that the "Additionalparam" parameter is missing and we are just getting an encoded URL.

To get the parent Id from URL of Lightning Component & Salesforce1 App  here is the code below : 

OpportunityPage.cmp

1
2
3
4
5
6
<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,lightning:actionOverride,lightning:isUrlAddressable" access="global" >
 <aura:attribute name="recordId" type="String"/>
 <aura:attribute name="accId" type="string"/>
    <aura:handler name="init" action="{!c.doInit}" value="{!this}" />
    <p style="font-size:40px">Parent ID : {!v.accId}</p> 
</aura:component>


OpportunityPageController.js


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
({
 doInit : function(component, event, helper) {
        //Get Parent Id from URL start 
        //
        //
        var pageReference = component.get("v.pageReference");
        console.log('###RecordId : '+pageReference.state.recordTypeId);
        var getAccId = pageReference.state.additionalParams;
        if(typeof getAccId !== 'undefined') {
            console.log('###GetAccID : '+getAccId);
            getAccId = getAccId.replace('accid=','');
            getAccId = getAccId.substring(0,15);
            console.log('###AccId : '+getAccId);
            component.set("v.accId", getAccId);
        }
        //
        //
        //Get Parent ID from URL end
        //
        //
        //Get Parent ID from URL in Salesforce1 App Start
        //
        //
        //
        var value = helper.getParameterByName(component , event, 'inContextOfRef');
        console.log('###Value='+value);
        var context = JSON.parse(window.atob(value));
        console.log('###context='+context);
        var newContext = JSON.stringify(context);
        console.log('###newContext : '+newContext);
        if(newContext.indexOf("address")>0){
            console.log('###Context : '+context.attributes.attributes.address);
            var url =context.attributes.attributes.address;
            console.log('###URL='+url);
            var n = url.indexOf("accid%3D");
            var subStr = url.substring(n+8);
            console.log('###Sub Str='+subStr);
            var accId = subStr.substring(0,15);
            console.log('###AccId : '+accId);
            component.set("v.accId", accId);
        }
        else{
            console.log('##Its not a Salesforce1 App');
        }
        //
        //
        //Get Parent ID from URL in Salesforce1 App end
        
 }
})


OpportunityPageHelper.js

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
({
 getParameterByName: function(component, event, name, url) {
        name = name.replace(/[\[\]]/g, "\\$&");
        var url = window.location.href;
        var regex = new RegExp("[?&]" + name + "(=1\.([^&#]*)|&|#|$)");
        var results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }
})


Note : Please open the console in your browser and check the logs carefully to understand it better.

Check the video below to understand it better






Please check below links also and subscribe if you like the content : 
If you have any question please leave a comment below.
If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 😊