7

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?

4

As of today, you cannot capture the "parent context" on a lightning component invoked from a related list. We have had a similar situation and found out that it is not possible and ended up creating this idea requesting for a feature around this.

The alternative that we came up was to create a custom button on the parent record itself and then create the related list records. We invoked the same component that was invoked from related list, but were able to pass the parent context in this case. While this worked for the use case that we had, but it may be necessarily true in your case, but this is one of the options you can try.

  • It would be much appreciated if I can get to know the reason for the downvote. The answer does provide the op with the answer and also based on a similar situation of an alternative. – Jayant Das Jul 3 '18 at 21:34
  • I didn't downvote, I think that the option you've described is the only way to resolve this issue. I was afraid that I would have to implement custom related list. Anyway, it isn't cool that Salesforce OOB override solution doesn't populate parent ID in any way. – jm. Jul 5 '18 at 16:33
  • @D.M. - That message was for whoever downvoted :), thanks for clarifying though. As for this one, yeah this is something which is required and that's what we mentioned in the idea. However if you keep a button on the parent record itself and then take it from there to create a related list item, should help here. – Jayant Das Jul 5 '18 at 16:42
6

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);
    }
})
  • Works for desktop. In Salesforce1, pageRef currently returns "{"type":"standard__objectPage","attributes":{"objectApiName":"Opportunity","actionName":"new"},"state":{}}" => notice empty state – Christian Szandor Knapp Mar 6 at 20:27
  • Amazing!........ – salesforce-sas Oct 16 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.