8

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?

1
15

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);
    }
})
3
  • 1
    Works for desktop. In Salesforce1, pageRef currently returns "{"type":"standard__objectPage","attributes":{"objectApiName":"Opportunity","actionName":"new"},"state":{}}" => notice empty state Mar 6 '19 at 20:27
  • Amazing!........ Oct 16 '19 at 5:54
  • Awesome. Thank you. Aug 7 '20 at 1:31
5

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.

3
  • 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
0

If your use case permits, another solution might be to use sessionStorage. Aside from your custom action override component, you will need one additional component.

CaseOverride.cmp - your custom "New Case" form, which you assign to the New override for Case. This is the one where you currently can't get the Account Id for a new Case.

CaseOverrideHelper.cmp - a component you place in the Account Lightning page. It has one job: to capture the Account recordId and store it in a session variable.

Each time your Account Lightning page loads, your session variable will be updated with the current Account Id and available to your New Case action override component, and any other component that needs it.

CaseOverrideHelper.cmp

<aura:component implements="flexipage:availableForAllPAgeTypes, force:hasrecordId">
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>

CaseOverrideHelperController.js

({
    doInit : function(component, event, helper) {
        // Get Account ID and place in storage
        sessionStorage.setItem("accountId", component.get("v.recordId"));
    }
      
})

Now, in your custom "New Case" override component (CaseOverride.cmp), you can retrieve the Account ID in the controller or helper like this:

    var accountId = sessionStorage.getItem("accountId");
3
  • @womanwhodevs Please do not reject my edit, which corrects code syntax.
    – Todd Gill
    Jul 13 at 19:43
  • I'm not rejecting your edit, I don't have enough reputation to accept or reject anyone's edits. Jul 14 at 20:43
  • Ah, I misunderstood the notification. Looks like you corrected a couple of my typos. Thanks!
    – Todd Gill
    Jul 16 at 18:59
0

I am also stuck with same thing

google below link which is working fine for me you can also try this

hope this work for you

click here

2
  • 1
    Welcome to Salesforce Stack Exchange (SFSE). Link-only answers are discouraged, and may be deleted. (See: Why and how are some answers deleted?) Please edit you answer to add more information. (From How do I write a good answer?: "...please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link.... ")
    – Moonpie
    Oct 4 at 12:06
  • 1
    Also, please explain how your answer (the info in the link) is different from Jon Duelfer's answer, because at first glance they appear to make the same suggestion.
    – Moonpie
    Oct 4 at 12:08

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.