0

I have created a aura component which is opening on button click in list view. Once i click on that button and create a record. Once record is created it navigate to another page. I go back to original listview and click on url button again. Component is loaded but with same values that i used to create last record.

I tried using disconnectedCallback but that is not fired in this whole process. I also want to call some method when component come to screen again( connectedCallBack). Is there any event i can listen to reset or reload my component.

Here is sample code to test this behaviour

Aura testing.cmp

<aura:component implements="lightning:isUrlAddressable">
    <c:testingLwc></c:testingLwc>
</aura:component>

Lwc testingLwc.html

<template>
    <lightning-input label="Last Name" value={lastName} onchange={change}></lightning-input>
    <lightning-button label="Create" onclick={createContact}></lightning-button>
</template>

lwc testingLwc.js

import { LightningElement } from 'lwc';
import { createRecord } from 'lightning/uiRecordApi';

import { NavigationMixin } from 'lightning/navigation';

export default class TestingLwc extends NavigationMixin(LightningElement) {
    lastName;

    connectedCallback(){
        this.lastName = 'testing';
    }
    change(event){
        this.lastName = event.detail.value;
    }
    createContact(){
        const fields = {};
        fields.LastName = this.lastName;
        const recordInput = { apiName: 'Contact', fields };
        createRecord(recordInput)
        .then(con => {
            this[NavigationMixin.Navigate]({
                type: 'standard__recordPage',
                attributes: {
                    recordId: con.id,
                    objectApiName: 'Contact',
                    actionName: 'view'
                }
            });
        });
    }

    disconnectedCallback(){
        console.log('testing');
    }
}

Metadata for url button

<?xml version="1.0" encoding="UTF-8"?>
<WebLink xmlns="http://soap.sforce.com/2006/04/metadata">
    <fullName>Assign</fullName>
    <availability>online</availability>
    <displayType>massActionButton</displayType>
    <encodingKey>UTF-8</encodingKey>
    <height>600</height>
    <linkType>url</linkType>
    <masterLabel>Assign</masterLabel>
    <openType>sidebar</openType>
    <protected>false</protected>
    <requireRowSelection>false</requireRowSelection>
    <url>/lightning/cmp/testing</url>
</WebLink>

What i am trying to do is find a event on which i can reset my component to some state. Like in this demo use case reset this.lastName = 'testing';

1 Answer 1

0

1 solution that i have right now is sending a event to aura component and using $A.get('e.force:refreshView').fire(); in aura to refresh component. This is just a workaround and not an ideal solution.

createContact(){
        const fields = {};
        fields.LastName = this.lastName;
        const recordInput = { apiName: 'Contact', fields };
        createRecord(recordInput)
        .then(con => {
            this[NavigationMixin.Navigate]({
                type: 'standard__recordPage',
                attributes: {
                    recordId: con.id,
                    objectApiName: 'Contact',
                    actionName: 'view'
                }
            });
            this.dispatchEvent(new CustomEvent('refresh', {
                composed: true,
                bubbles: true,
                cancelable: true
            }));
        });
    }

and in Aura

<aura:component implements="lightning:isUrlAddressable">
    <c:testingLwc onrefresh="{!c.refreshComponent}"></c:testingLwc>
</aura:component>


({
    refreshComponent: function(){
        $A.get('e.force:refreshView').fire();
    }
}) 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .