0

How to get parent id on custom button click of the related list.

Question Exploration:- when we open the Account detail record page and go in the related tab we have a contact list there and a new button on the contact list tile...when we click that new button new record modal is open with a pre-populated account in it.

so, I have to create a custom button that does this same thing.

  • Go to https://ideas.salesforce.com/s/idea/a0B8W00000Gdb0LUAR/get-parentid-when-overriding-standard-actions-with-a-lightning-components and load older comments few times. There are some code examples for Aura, you'd need to adapt them for LWC. Basically the parent Id should be somewhere in that base64-encoded thing after "#" in the address bar. And of course upvote the idea ;) – eyescream Mar 08 '22 at 16:45

1 Answers1

0

When you click your custom button, the context is passed in the URL as a variable named inContextOfRef and the value is a base64-encoded string. You can get this value from the URL and decode it in your component. For LWC, you could do something like this:

import { LightningElement } from 'lwc';
export default class MyCoolLWC extends LightningElement {
    // this variable will contain the parent record Id
    recordId;   
    
    // this executes when your LWC is loaded
    connectedCallback() {
        const params = new Proxy(new URLSearchParams(window.location.search), {
            get: (searchParams, prop) => searchParams.get(prop)
        });
        let inContextOfRef = params.inContextOfRef;
        if (inContextOfRef.startsWith("1\.")) { inContextOfRef = inContextOfRef.substring(2); }
        var addressableContext = JSON.parse(window.atob(inContextOfRef));
        this.recordId = addressableContext.attributes.recordId;
    }
}
Chris Hubbard
  • 554
  • 5
  • 11