Component is not refresh during 2nd attempt after clicking on new button

auralightning-eventslightning-recordeditformlightning-web-components

I have overridden Standard New button on lead object with Aura Component and this Aura calls LWC which contains record edit form to create new lead record.

I have 2 record types Record Type 1 & Record Type 2 defined on Lead Object. So during record creation, If I select Record Type 1 and then cancel the operation, I am able to navigate back to Lead List views. But, when I click on New Button again and this time If I select Record Type 2 and create a record then Lead records gets created in the system with Record Type as Record Type 1. It should have created Record with Record Type as Record Type 2.

I have observed that when I click on New button 2nd time , it does not refresh the component.

Below is the Sample Code that I have written:

Aura :

<aura:component
   implements="lightning:actionOverride,force:lightningQuickActionWithoutHeader,force:hasSObjectName,lightning:hasPageReference"
    access="global">
    <aura:attribute name="selectedRecordId" type="Id" />
    <aura:attribute name="objectName" type="String" />
    <!--Declare Handler-->
    <aura:handler name="init" value="{!this}" action="{!c.doInit}" />
    <div>
        <c:newLeadRecord recordTypeId="{!v.selectedRecordId}" SFDCobjectApiName="{!v.objectName}" />
    </div>
</aura:component>

Aura Controller :

({
    doInit: function(component, event, helper) {
        //get record type Id
        var recordTypeId = component.get("v.pageReference").state.recordTypeId;
        component.set("v.selectedRecordId", recordTypeId);
        //get object API name
        var objectApiName = component.get("v.pageReference").attributes.objectApiName;
        component.set("v.objectName", objectApiName);
    }
})

LWC HTML:

<lightning-record-edit-form object-api-name={sfdcObjectApiName} record-type-id={recTypeId} onsuccess={handleSuccess}
        onerror={handleError} density="comfy">
        .....  .........
</lightning-record-edit-form>

Can someone please help me what how to refresh component everytime when you click on Standard New Button? or how to refresh everytime when I cancel the operation or successfully created the record?

Best Answer

Try to add this to the Renderer.js of your Aura:

rerender : function(component, helper) {
  this.superRerender();
  let recordTypeId = component.get("v.pageReference").state.recordTypeId;
  component.set("v.selectedRecordId", recordTypeId);
}

This should be called automatically each time your Aura appears on the screen.

Related Topic