How to create child record with Parent record information using Salesforce Quick Action?

How to create child record with Parent record information using Salesforce Quick Action?

To create child record with Parent record information using Quick Action in Salesforce, we can use

1. force:recordData to get Parent record information

2. e.force:createRecord to create child record

Sample Code:

Lightning Aura Component:

<aura:component implements="force:hasRecordId,force:lightningQuickActionWithoutHeader" >
    <aura:attribute name="caseRecordError" type="String"/>
    <aura:attribute name="recordFields" type="Object" />
    <force:recordData 
                      recordId="{!v.recordId}"
                      aura:id="caseRecordCreator"
                      targetFields ="{!v.recordFields}"
                      targetError="{!v.caseRecordError}"
                      layoutType="FULL"
                      fields="Id,Name"   
                      recordUpdated="{!c.handleRecordUpdated}" 
                      />
</aura:component>

Lightning Aura Component JavaScript Controller:

({
    
    handleRecordUpdated : function(component, event, helper) {
        
        let closeActionPanel = $A.get( "e.force:closeQuickAction" );
        closeActionPanel.fire();
        let createCaseFromAccount = $A.get( "e.force:createRecord" );
        let accountRec = component.get( "v.recordFields" );     
        let accountId = component.get( "v.recordId" );
        let caseInternalRecTypId = $A.get( "$Label.c.CaseInternalRecordType" );
        console.log( 'caseInternalRecTypId is', caseInternalRecTypId );
        
        createCaseFromAccount.setParams( {
            "entityApiName": "Case",
            "defaultFieldValues": {
                'AccountId' : accountId,
                'Subject' : accountRec.Name + ' Case'
            },
            "recordTypeId" : caseInternalRecTypId
        } );
        createCaseFromAccount.fire();
        
    }
    
} )

Quick Action:

Output:

Leave a Reply