+ Start a Discussion
monalisa das 46monalisa das 46 

How to get record Id from Lightning record edit form, when used for record insert?

I have a requirement where I am making use of lightning:recordEditForm to create a new record. I am using standard save functionality. After successful save of record I need to make use of the record Id. Is there is any native way to get Id of record without making server call?
 
<lightning:recordEditForm  onsuccess="{!c.handleSuccess}" onerror="{!c.handleError}"
        aura:id="recordEditForm" 
      objectApiName="Address__c">

            <lightning:messages />

           <lightning:inputField fieldName="Customer__c" value="{!v.accountRecord.Id}" class='slds-hide'/> 

            <aura:iteration items="{!v.displayListAddress}" var="item" >
                <lightning:inputField fieldName="{!item}" onchange="{!c.fieldValChange}"/>                             
            </aura:iteration>  
        <div class="slds-modal__footer">
           <div class="slds-align_absolute-center">
              <lightning:button label="Cancel" title="Neutral action" onclick="{! c.closeEdit }"/>
              <lightning:button variant="brand" type="submit" name="save" label="Save" />
           </div>
        </div>

I thought that after successful record creation the recordId attribute will get populated with appropriate value and will give proper value via this
alert("Id : "+component.find("recordEditForm").get("v.recordId"));

But this returns undefined.
Though I can make a server call with appropriate filters in SOQL to get Id of the record, but seems there should be some easier way around without server call.
Ravi Dutt SharmaRavi Dutt Sharma
Hi Monalisa,

To get the record id, add below code to your JS controller.
 
({
    handleSuccess : function(component, event, helper) {
        var payload = event.getParams().response;
        console.log(payload.id);
    }    
})

Please mark this as the best answer if it resolves your issue. Thanks.
Raj VakatiRaj Vakati
({
    handleSuccess : function(component, event, helper) {
        var payload = event.getParams().response;
        console.log(JSON.stringify(payload));
    }    
})
https://developer.salesforce.com/docs/component-library/bundle/lightning:recordEditForm/documentation
({
    handleSuccess : function(component, event, helper) {
        var payload = event.getParams().response;
        console.log(payload.id);
    }    
})