URL hack workaround using lightning component

In Classic, URL hack is used by everyone and there are so many scenarios to use URL hack. One of the most used scenarios is to avoid the Record Type selection in the Custom Links/Buttons.

In the Summer ’17 release, Salesforce introduced Navigate to Custom Tabs More Easily with Friendly Tab URLs

This functionality allows us to use Lightning Component for URL Hacks. For this new approach, we have to create a Lightning Component and a Lightning Component Tab that refers to the Component

From the Developer Console create a Lightning Component  and name it, for example “recordTypeSelectionCmp” and paste the below code

<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId,force:lightningQuickAction,force:appHostable" access="global" >
 
 <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
 
</aura:component>

Click the Controller and paste the below code

({
  doInit : function(component, event, helper) {
    var PageURL = decodeURIComponent(window.location.search.substring(1)); //You get the whole decoded URL of the page.
        var URLVariables = PageURL.split('&'); //Split by &amp;amp; so that you get the key value pairs separately in a list
        var ParameterName;        
        var objectName;
        var recordType;
    console.log(URLVariables)
        for (var i = 0; i &amp;lt; URLVariables.length; i++) {
            ParameterName = URLVariables[i].split('='); //to split the key from the value.			
            if (ParameterName[0] === 'recordType') { //getting the recordType Info
               recordType = (ParameterName[1]  === undefined ? 'Not found' : ParameterName[1]);            }
            else if (ParameterName[0] === 'objectName') { //getting the object Info
                objectName = (ParameterName[1] === undefined ? 'Not found' :ParameterName[1]);
            }
        }
        var createRecordEvent = $A.get("e.force:createRecord");
        createRecordEvent.setParams({
            "entityApiName": objectName,
            "recordTypeId" : recordType
        });
        createRecordEvent.fire();
  }
})

Now create the Lightning Tab from setup/Home and find “Tabs” in the Quick find.

Provide Tab Name, Tab Label, Tab Style and select the created component from the Lightning Component Picklist and save the tab.

Next, open the tab with the recordType and objectName variable in the URL, Please note ObjectName should be exact API name for example:

www.yourDomainName.lightning.force.com/one/one.app?recordType=01241000001ELa0AAG&objectName=Account#/n/recordType

Once you open the URL, you will see Record Type will be auto-populated.

4 thoughts on “URL hack workaround using lightning component

Leave a Reply

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