function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Navin Ganeshan 10Navin Ganeshan 10 

aura: populate account when creating contact from account related list

Hi,

Looking from some guidance on how to pre-populate account id on a custom aura component to create contacts. 

I would like to pre-populate account id on the component, when a user clicks on "New Contact", on Account's related contact list. I have declared "force:hasRecordId on the component. but nothing is returned for recordId:
 
<aura:component implements="force:lightningQuickAction,force:hasRecordId">  
     <lightning:input value={!v.recordId}
</aura:component>
However, this does work when i add new contact button to Account's record detail page. 

Any idea why this would not work from related page?

Thanks,
Navin
 
Best Answer chosen by Navin Ganeshan 10
Maharajan CMaharajan C
Hi Navin,

force:hasRecordId interface will works only if the custom component is used on a Lightning record page. But the same won't work for the related list.

As an alternate you can use the lightning:isUrlAddressable and Pagerefence in lightning. Please refer the below sample codes.
https://sfdclesson.com/2019/11/23/get-parent-record-id-from-lightning-url/

Example
<aura:component implements="flexipage:availableForRecordHome,flexipage:availableForAllPageTypes,force:hasRecordId,lightning:actionOverride,lightning:isUrlAddressable">
    <aura:attribute name="recordId" type="Id"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>
 
({
    doInit : function(component, event, helper) {
        var pageRef = component.get("v.pageReference");
        var state = pageRef.state; 
        var base64Context = state.inContextOfRef;
         if (base64Context.startsWith("1\.")) {
            base64Context = base64Context.substring(2);
        }
        var addressableContext = JSON.parse(window.atob(base64Context));
        component.set("v.recordId", addressableContext.attributes.recordId);
})


Thanks,
Maharajan.C

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Navin,

>> https://developer.salesforce.com/forums/?id=9060G0000005SiCQAU

I have found the above thread that has a similar implementation and I added a line to show the Record Id, it is in bold. You can use the below code to prepopulate the account id on click of a custom create contact button:
 
<aura:component controller="CreateRelatedcontact" 
                implements="force:appHostable,flexipage:availableForAllPageTypes,
                            flexipage:availableForRecordHome,
                            force:hasRecordId,forceCommunity:availableForAllPageTypes,
                            force:lightningQuickAction" 
                access="global" >
    
    <aura:attribute name="recordId" type="Id"/>
    <aura:attribute name="Name" type="String"/>
    <aura:attribute name="account" type="Account"/>
    <aura:attribute name="contact" type="Contact"
                    default="{ 'sobjectType': 'Contact','LastName':'','FirstName':'','Name'	:'','AccountId':''}"/>
    
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> 
    <div class="slds-form-element__control">
        <lightning:input name="RecordId"  label="record Id" value="{!v.recordId}" disabled="true"/>
        <lightning:input name="Account Name"  label="Account Name" value="{!v.account.Name}" disabled="true"/>
    </div> 
    <div class="slds-form-element__control">
        FirstName  <force:inputField value="{!v.contact.FirstName}"/>
    </div>
    <div class="slds-form-element__control">
        LastName  <force:inputField value="{!v.contact.LastName}"/>
    </div>            
    <lightning:button aura:id="insert" label="Create Contact" class="slds-button slds-button--brand buttontype" onclick="{!c.InsertCont}" />  
    
</aura:component>
 
({    
    doInit : function(component, event, helper) {
        var accid = component.get("v.recordId");
        component.set("v.contact.AccountId",component.get("v.recordId"));
        var action=component.get("c.getAccount");
        action.setParams({
            acid : component.get("v.recordId")            
        });
        action.setCallback(this, function(response){
            var state = response.getState();
            if (state === "SUCCESS"){
                component.set("v.account",response.getReturnValue());
            }
        });
        $A.enqueueAction(action);
        
    },
    
    InsertCont : function(component){
        
        var action=component.get("c.insertCon");
        action.setParams({ "newcon": component.get("v.contact") });
        action.setCallback(this, function(response)
                           {
                               var state = response.getState();
                               if (state === "SUCCESS") 
                               {
                                   var contactid=response.getReturnValue().Id;
                                   alert("contact created.Id:"+contactid)
                                   var toastEvent = $A.get("e.force:showToast");
                                   toastEvent.setParams({
                                       "title": "Success!",
                                       "message": "Contact record Created successfully.",
                                       "mode":"pester"
                                   });
                                   toastEvent.fire();
                                   
                                   var navEvt = $A.get("e.force:navigateToSObject");
                                   navEvt.setParams({
                                       "recordId": contactid,
                                       "slideDevName": "related"
                                   });
                                   navEvt.fire();
                               }
                           });
        $A.enqueueAction(action);
    }
})
 
public class createrelatedcontact 
{
@AuraEnabled
    public static contact insertCon(Contact newcon)
    {
        insert newcon;
        return newcon;        
    }
    @AuraEnabled
    public static Account getAccount(string acid)
    {
        Account acc = [select id,name from Account where Id =:acid];
        return acc;
    }
}

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Navin Ganeshan 10Navin Ganeshan 10
Thanks but I am finding this weird... this works when I place a custom button on a record page but does not work when it is called from the Contact section in the related list tab of Account's record page...
Maharajan CMaharajan C
Hi Navin,

force:hasRecordId interface will works only if the custom component is used on a Lightning record page. But the same won't work for the related list.

As an alternate you can use the lightning:isUrlAddressable and Pagerefence in lightning. Please refer the below sample codes.
https://sfdclesson.com/2019/11/23/get-parent-record-id-from-lightning-url/

Example
<aura:component implements="flexipage:availableForRecordHome,flexipage:availableForAllPageTypes,force:hasRecordId,lightning:actionOverride,lightning:isUrlAddressable">
    <aura:attribute name="recordId" type="Id"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
</aura:component>
 
({
    doInit : function(component, event, helper) {
        var pageRef = component.get("v.pageReference");
        var state = pageRef.state; 
        var base64Context = state.inContextOfRef;
         if (base64Context.startsWith("1\.")) {
            base64Context = base64Context.substring(2);
        }
        var addressableContext = JSON.parse(window.atob(base64Context));
        component.set("v.recordId", addressableContext.attributes.recordId);
})


Thanks,
Maharajan.C
This was selected as the best answer
Maharajan CMaharajan C
https://salesforce.stackexchange.com/questions/223766/lightning-new-case-action-override

Thanks,
Maharajan.C
Navin Ganeshan 10Navin Ganeshan 10
Thanks Maharajan... that was it!!!
Rajashekar ShetipallyRajashekar Shetipally
Thanks Maharajan it is helpful