Button Overrides – Lightning New Case Action Override

I have action override on New Case button which runs our custom component.
As you know, we can create a new Case from different places in org, e.g. from related lists. When creating a Case from related list on Account using standard action in Lightning we have prepopulated AccountId to the lookup field. Is it possible to do the same with custom component? I can't catch the AccountId using force:hasRecordId, because action override redirects me to the new page with custom component where I don't have a context of Account. URL also doesn't contain its Id. What can I do to grab Id of context object and populate it to my component?

Best Answer

There is a solution that does indeed work, but it's a bit fragile because it's parsing the entire URL (assuming a structure that could change): https://developer.salesforce.com/forums/?id=9060G000000UaqdQAC (look at Pascal Le Clech's answer).

However, we can build on this idea and make it more structurally sound by using https://developer.salesforce.com/docs/component-library/bundle/lightning:isUrlAddressable to grab the new inContextOfRef which gives us a URL Addressable Base 64 encoded string. Here's a slightly more stable solution to Pascal Le Clech's solution:

aura:component

<aura:component implements="lightning:actionOverride, force:hasRecordId, lightning:isUrlAddressable">
    <aura:attribute name="recordId" type="Id"/>
    <aura:handler name="init" value="this" action="{!c.init}"/>
</aura:component>

controller.js

({
    init : function(cmp, event, helper) {
        var pageRef = cmp.get("v.pageReference");
        var state = pageRef.state; // state holds any query params
        var base64Context = state.inContextOfRef;

        // For some reason, the string starts with "1.", if somebody knows why,
        // this solution could be better generalized.
        if (base64Context.startsWith("1\.")) {
            base64Context = base64Context.substring(2);
        }
        var addressableContext = JSON.parse(window.atob(base64Context));
        cmp.set("v.recordId", addressableContext.attributes.recordId);
    }
})
Related Topic