Question
I am using an Aura component to override the New and Edit actions for a custom object (Child_Object__c), which has a master-detail relationship with Parent_Object__c. When clicking Edit, force:hasRecordId correctly provides the record Id of the child object. However, when clicking New from the related list on the parent record’s detail page, there is no recordId available. I need to retrieve the parentId to apply some logic.
Is there a way to get the parent record Id when overriding the New action in an Aura component? I’ve come across some workarounds, but I want to know if there’s a cleaner or more updated approach.
Answer
Currently, there is no direct way to retrieve the parent record Id when overriding the New action in an Aura component. The force:hasRecordId only provides the Id of an existing record, which means it works for Edit but not for New, since no record exists yet. However, there are some workarounds to extract the parentId from the URL context.
One effective workaround is to use the inContextOfRef parameter, which stores the previous page reference information in a base64-encoded format. You can decode this value to extract the parent record Id.
Approach Using Aura + LWC
Since lightning:actionOverride is not available in LWC yet, the workaround involves wrapping an LWC inside an Aura component. The Aura component handles the override, while the LWC extracts the parentId and navigates correctly.
Aura Component (Wrapper for LWC Override)
<aura:component
description="genericActionOverride"
implements="lightning:actionOverride, lightning:isUrlAddressable">
<!-- Delegating to the LWC for handling logic -->
<c:genericActionOverrideDelegate pageReference="{!v.pageReference}"></c:genericActionOverrideDelegate>
</aura:component>LWC (Extracting Parent Id and Navigating Correctly)
import { LightningElement, api } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import { encodeDefaultFieldValues } from "lightning/pageReferenceUtils";
import PARENT_ID_FIELD from '@salesforce/schema/<<objectApiName>.<<fieldApiName>>';
export default class GenericActionOverrideDelegate extends NavigationMixin(LightningElement) {
@api
set pageReference(value) {
this._pageReference = value;
this.parentRecordId = null;
if (this.pageReference) {
let base64Context = this.pageReference?.state?.inContextOfRef;
if (base64Context) {
if (base64Context.startsWith("1\.")) {
base64Context = base64Context.substring(2);
this.originalPageReference = JSON.parse(atob(base64Context));
// Extract the parent Id
const recordId = this.originalPageReference?.attributes?.recordId;
this.parentRecordId = recordId;
this.ready = () => this.connected && this.parentRecordId;
}
}
}
this.navigateWhenReady();
}
get pageReference() {
return this._pageReference;
}
_pageReference;
connected;
parentRecordId;
ready = () => false;
connectedCallback() {
this.connected = true;
this.navigateWhenReady();
}
async navigateWhenReady() {
if (!this.ready()) return;
const backgroundContext = await this[NavigationMixin.GenerateUrl](this.originalPageReference);
const defaultFieldValues = {};
defaultFieldValues[PARENT_ID_FIELD.fieldApiName] = this.parentRecordId;
const newPageReference = {
type: this.pageReference.type,
attributes: {
objectApiName: this.pageReference.attributes?.objectApiName,
actionName: this.pageReference.attributes?.actionName
},
state: {
backgroundContext,
defaultFieldValues: encodeDefaultFieldValues(defaultFieldValues),
nooverride: '1'
}
};
this[NavigationMixin.Navigate](newPageReference);
}
}Explanation of the Code
The given JavaScript code is an LWC (Lightning Web Component) that acts as a delegate for overriding the New action in Salesforce. The main goal of this component is to extract the parent record Id when creating a child record from a related list and then navigate to the standard Salesforce “New” record page with default values pre-filled.
The GenericActionOverrideDelegate LWC is designed to override standard actions in Salesforce while preserving the context of the parent record. It extends NavigationMixin to handle navigation dynamically. The component exposes a pageReference property, and when it’s set, it extracts the inContextOfRef parameter from the URL, which contains a base64-encoded reference to the original record. If present, the component decodes this reference, retrieves the recordId, and sets parentRecordId.
A ready function determines when the component is fully initialized. In connectedCallback, it ensures that navigation is triggered once the component is connected. The navigateWhenReady method waits for readiness, generates a background context URL from the original page reference, and constructs a new page reference that includes default field values, specifically setting the parent record ID for pre-population.
Finally, it navigates to the new action while ensuring the standard override behavior is bypassed using nooverride: '1'. This approach allows for seamless redirection while preserving relationships between records when overriding standard Salesforce actions.
Explanation of This Approach
The inContextOfRef parameter in the URL contains information about the previous page (in this case, the parent record). The LWC decodes this value and extracts the parent Id, which is then used to set the default field values before navigating to the “New” page.
Additionally, this method leverages the backgroundContext parameter, which ensures that when the user clicks “Cancel,” they are returned to the correct page instead of seeing an unexpected screen.
Alternative Approaches
Another approach is to pass the parentId using a custom button with a URL parameter. However, this method is not ideal in a Lightning Experience setup due to navigation restrictions and security concerns.
At present, the inContextOfRef workaround remains one of the best solutions available. If Salesforce introduces a more native way to retrieve the parent Id for a New action override, that would be preferable.
Enroll for Career-Building Salesforce Training with 100% Money Back Guarantee
Our Salesforce Course is meticulously designed to provide a deep understanding of the Salesforce platform, equipping you with the essential skills to excel in the CRM industry. The program includes vital modules such as Salesforce Admin, Developer, and AI, combining theoretical insights with hands-on training. Through practical assignments and real-world projects, you’ll gain the expertise to solve complex business challenges using Salesforce solutions. Our experienced instructors ensure you acquire both technical proficiency and industry-specific knowledge to succeed in the Salesforce ecosystem.
In addition to technical training, our Salesforce training in Kochi offers dedicated mentorship, certification guidance, and interview preparation to enhance your career prospects. You’ll have access to detailed study materials, live project experience, and continuous support throughout your learning journey. By the end of the program, you’ll be well-prepared for certification exams and possess the practical problem-solving skills that employers seek. Take the first step in your Salesforce career today and unlock limitless opportunities. Enroll for a Free Demo now!

Leave a Reply
You must be logged in to post a comment.