0

I have an parent component that fires an Apex Method.

The method grabs data and passes to the child component, then the child component runs some logic and fires an alert.

The weird thing is that when the record where the LWC lives is updated, the child component fires but the parent component does not. This is a problem because if the user makes an update from the LWC child component, I need the Parent Component to run it's apex code again. Even using refreshApex() seems to not call the parent component. It seems that the @wire method is overriding the parent component on updates because the opportunity data is being changed and so it's skipping my parent component and firing the wire method from the cache...not sure if this is the case, but that's my theory.

Here is the code.

Parent.JS

import { LightningElement, api, wire, track } from 'lwc';

import getCTMData from '@salesforce/apex/LWC_ContactController.getData';

import USER_ID from '@salesforce/user/Id';

import CUSTOMER_CONTACT_ID_FIELD from '@salesforce/schema/Opportunity.Customer_Contact__c';
const FIELDS = [
    CUSTOMER_CONTACT_ID_FIELD
]

export default class ParentComp extends LightningElement {

    @api recordId;
    @api userId = USER_ID;
    @track record;

async connectedCallback() {
        console.log('CONNECTED CALL BACK');
    this.record = await getData({userId: USER_ID, oppId: this.recordId});

    console.log('CONNECTED CALL BACK END');

      }

Child.JS

import { LightningElement, api, wire, track } from 'lwc';
import { getRecord, getFieldValue } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';

//import fields...


const FIELDS = [
    CUSTOMER_CONTACT_ID_FIELD,
    CUSTOMER_CONTACT_FIRST_NAME_FIELD,
    CUSTOMER_CONTACT_LAST_NAME_FIELD,
    CUSTOMER_CONTACT_NAME_FIELD,
    CUSTOMER_CONTACT_EMAIL_FIELD,
    PRIMARY_PHONE_FIELD,
    PHONE_NUMBER_2_FIELD,
    PHONE_NUMBER_3_FIELD
];

export default class ChildComponent extends LightningElement {
@api recordId;
    @api ctmPhoneNumber;
    @api ctmActivityRecord;

@wire(getRecord, {recordId: '$recordId', fields: FIELDS})
    oppRecord({data,error}){
        // this.wiredOppResult = result;
        // let data = result.data;
        // let error = result.error;
        if(data){
//THIS KEEPS GETTING CALLED ON UPDATE BUT PARENT DOES NOT...?
            console.log('OPP: Data Detected');
            console.log(data)
            //Build contact object
            this.conObj.recordId = getFieldValue(data, CUSTOMER_CONTACT_ID_FIELD)
            this.conObj.name = getFieldValue(data, CUSTOMER_CONTACT_NAME_FIELD);
            this.conObj.firstname = getFieldValue(data, CUSTOMER_CONTACT_FIRST_NAME_FIELD);
            this.conObj.lastname = getFieldValue(data, CUSTOMER_CONTACT_LAST_NAME_FIELD);
            this.conObj.phones.primaryPhone = formatPhoneBare(getFieldValue(data,PRIMARY_PHONE_FIELD));
            this.conObj.phones.phone2=formatPhoneBare(getFieldValue(data, PHONE_NUMBER_2_FIELD));
            this.conObj.phones.phone3 = formatPhoneBare(getFieldValue(data, PHONE_NUMBER_3_FIELD))
            this.conObj.email = getFieldValue(data, CUSTOMER_CONTACT_EMAIL_FIELD);


//Handlers...{}

}

  • 1
    You're missing code from the parent that is relevant to your question. I see no reason why the parent should be updated in the current situation, because you're not calling refreshApex, you don't have a wire method in the parent, and you don't have an event handler to listen to the child for an update. You'd need one of these three things to get the behavior you expect. LWC cannot automatically assume that the Apex method is at all related to the wire method, even on the same record Id. You need to do something specific to get the parent to update. – sfdcfox Jul 6 at 0:59
  • @sfdcfox I had been calling the refreshApex() from an dispatch event in the child component. But the update to the opp record this lwc is on, would not provision more data. Instead, only the wire method in the child would run. My thought was that running refresh from a dispatched event prior to the update would have caused the parent component to rerun on update with the refresh data now in the cache. But it only ran the child component. – thinker Jul 6 at 1:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.