You need to sign in to do that
Don't have an account?

Can't retrieve a related parentid when overriding standard actions with a lightning components
I have a lightning component that implements lightning:actionOverride how do I get the recordId of the parent when the action is triggered from a related list?
To get the recordID you need to implement interfacte force:hasRecordId along with lightning:actionOverride.
force:hasRecordId - This enables the component to be assigned the ID of the current record.
You can get the recordId when the component is loaded in the init method and can perform the further logic.
Below is the sample code -
For example -
smapleComponent.cmp sampleComponentController.js Remember - Whenever you implement force:hasRecordId an attribute is added internally named as recordId which you can access in the controller or component. In the above example it is accessed in the controller JS 'LINE 4'.
Helpful links -
lightning:actionOverride (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_interfaces_lightning_actionoverride.htm?search_text=lightning:actionOverride)
force:hasRecordId (https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_interfaces_force_hasrecordid.htm?search_text=force:hasRecordId)
Note - Kindly mark this answer as best answer if this helped you, so that other's can also get benefit from it.
Thanks,
Mustafa Jhabuawala
Technical Lead at Zen4orce (http://www.zen4orce.com)
As per my understanding you need to pass the values to the standard field id's in the url i.e Salesforce URL hacking.
OR you can get the Id of the parent as below
1. get the URL of current page when you click on overriden action
string strURL= string.valueof(ApexPages.currentPage());
string strId='';
if(strURL.contains('=001')){ // this is id for account (you can get first 3 number from id of you parent record)
strId= strURL.substringBetween('=001','&'); // find parent id from string url
strParentId = ('001'+strId).trim(); // concatinate with 3 digits
}
I hope this helps you to get ParentId, let me know if you need additional help.
Thanks,
Sukanya Banekar
Salesforce Developer
http://www.zen4orce.com
I found your question interesting and challenging one.
Me and Sukanya have tried multiple possible ways to acheieve the functionality as you have mentioned.
Following are the findings -
1. I noticed that when you override the New action with a lightning component, and when you click on that 'New' button it navigates to a different URL, so in this case we won't be able to use URL hacking to get the parent Id from URL.
2. Instead of overriding the standard new action, you can create a new action button. Please refer below screen shot
I know its a quite different approach but this is how you can achieve this. So clicking on the new action button you will get the details in recordID for which you have to implmenet force:hasRecordId in your custom component.
So bascially the alternative approach would be to use action button.
Note - Kindly mark this answer as a best answer if this helped you, so that other's can also get benefit from it.
Thanks,
Mustafa Jhabuawala
Techincal Lead at Zen4orce (http://www.zen4orce.com)
If developers are going to override the "New" action, they will more than likely need to access the parent Id. Without it you can't establish the context of the action and attach your new record to parent. I've inspected the value provider and referring URL from inside the lightning component and the source (or parent) record ID doesn't seem to be set anywhere. Any other ideas/suggestions?
The same is happening with me. As created a new object which is in relationship of account where account is master object. New button of child obeject override with a lightning component. Code is given below
<aura:component implements="force:hasRecordId,lightning:actionOverride" controller="Customer_Issue_New_Record_cls" access="global">
<aura:attribute name="newIssueObject" type="xCustomer_Issue__c" default="{ 'sobjectType': 'xCustomer_Issue__c' }"/>
<aura:attribute name="recordId" type="String" default=""/>
<aura:handler name="init" value="{!this}" action="{!c.initSetup}" />
</aura:component>
({
initSetup : function(component, event, helper) {
console.log('------accountid-------'+component.get("v.recordId"));
}
})
Thank you
Dileep
Note that I can retrieve the recordId when the Lightning Component is launched from a Global Quick Action. My question is specific to the situation where the Quick Action replaces the New button on the related list. In this situation, recordId comes up as undefined.
Thanks,
Mike
Good luck! :)
I created list button with url type. But didn't get child record page in Lightning. Do you have an example url to populate here.
Thanks
If we click New from Contact tab it will just assign RecordTypeId if record type available.
If we click New from Related List under Account we will get accid from url. we can pass this to Lightning component attributes.
Let me know if you have any queries.
in component in controller :
in helper :
You're welcome ! 😉
If recordID is null you can get
if ( response.recordId ==null)
{
var ParentURL = response.pageReference.state.ws ;// or url parameter of response as response.url
//var context = response.pageReference.state.inContextOfRef ;
cmp.set("v.relatedURL",ParentURL);
//split the parentURL and get corresponding value for example
//cmp.set("v.ParentSObjectType",ParentURL.split("/")[3]);
//cmp.set("v.ParentSObjectId",ParentURL.split("001")[0]);
}
Again I just tried this and it worked for me, your specific case I might need relevant details to answer better
This means, as a developer you can't know the origin of the request. For example, you may have a Lightning Component override for New Contact. And now, if you try to create a New Contact from Account record, from contacts related list, you're Lightning Component have no way to read the Account ID and populate it when New Contact interface is loaded in your custom component.
As for your code, can you guide me, where does your "response" variable coming from? Ina lightning component, if we need record id, I think we read it by component.get("v.recordId").
I was able to do it in a Lightning experience with the console app. with the code I gave , could you give your component/app hierarchy as a sample and I can verify and let you know , Also are you checking recordid in init, if so let me know .
It is the only parameter which will work and you can get the param
For example
ws=/lightning/r/Account/001V000000YYPduIAH/view ( using url formula)
{!URLFOR( $Action.Case.NewCase , null,[ws=/lightning/r/Account/'+Case.AccountId'+ '/view' ]) }
get the param ws from window.location.href and then parse the url for the id.
Regards
Saranyan Narayanan
I've full code here, you can try it with as a Lightning Component and override Contact New button, and then try creating a New Contact from Account's Contact Related List.
Creating custom button is not valid workaround, One cannot replace all the places where New button appears with a custom button.
And it is an inconsistent behavior since the parent record id is passed to lightning component override if we have a Record Type on the object. so if I create Record Types on Contact objectg I can then have Account Id available in URL when creating the New Contact using same code for lightning component.
Also, if we create a new custom quick action then will it be possible to replace it with standard New button in related list of Parent object?
component:
------------------- controller:
-------------------
Create a quickaction for the component and accessing the url from init function of that component is one line code.
The above code gives u the full url from that url we can get Id of the parent record.
If it works mark my answer as Best.
Please check below blog post for reference :
https://www.salesforcebolt.com/2020/04/get-parent-id-salesforce1-Lightning-Component.html
"Uncaught Action failed: c:NewQuotePrepopulated$controller$doInit [Unexpected token ž in JSON at position 0]
Callback failed: serviceComponent://ui.force.components.controllers.createRecordTypeChecker.CreateRecordTypeCheckerController/ACTION$doRecordTypeCheck"
Has anyone else encountered this error? If so, how did you resolve? Thanks for your help!
Got the same error after saving the record using @Pascal's solution. Have you found any solution/ workaround? please share...
Thanks,
Rahul:)
Did you come up with any solution for the Save and New button ? I'm stuck on same.
Thanks,
IJ