0

Hello Everyone can anyone pls help why I am not able to console.log(apexParam), I am not even getting any alert and my FireEvent is also not getting fired

Cmp which is firing the event

HTML

<template>
    <ul class="slds-has-dividers_bottom slds-has-block-links_space" >
        <li class="slds-item board-item productRow">
            <div class="slds-clearfix">
                <div class="slds-float_left slds-align_absolute-center">
                    <a onclick={navigateToDetail} target="_blank">{account.Name}</a>
                    Accid id  {account.Id}
                </div>
                <div class="slds-float_right slds-align_absolute-center">
                    <button class="slds-button slds-button_success" onclick={showRelatedList} name="contact">View Contacts</button>&nbsp;&nbsp;&nbsp;&nbsp;
                    <button class="slds-button slds-button_success" onclick={showRelatedList} name="Opportunity">View Opportunites</button>
                </div>
            </div>
        </li>
    </ul>
</template>

JS file

export default class AccSearchResultWithCon extends LightningElement {
    @api account;

    @wire(CurrentPageReference) pageRef;

    showRelatedList(event){
        let buttonName = event.target.name;
        let accId = this.account.Id;
        console.log('Account Id',accId);
        console.log('Button Name',buttonName);
        if(buttonName ==='contact' || buttonName ==='Opportunity'){
            //alert('Button Name '+buttonName);
            let apexParam = {
                "aId" : accId,
                "btName" : buttonname
            };
            console.log('This is the apex param ',apexParam);
            alert(apexParam);
            fireEvent(this.pageRef, 'relatedlist' , apexParam);
        } 
     }
}

CMP who is subscribing the event

HTML

<template>
    Hi this is the accont Id {accountId}
</template>

JS File

export default class RealtedListTable extends LightningElement {
    @track accountId;

    @wire(CurrentPageReference) pageRef;

    connectedCallback() {
        registerListener('relatedlist', this.relatedListHandler, this);
    }

    relatedListHandler(data){
        let accid = data.aId;
        this.accountId = data.aId;
        console.log('This is the accId from Event',accid);
    }
    
    disconnectedCallback() {
        unregisterAllListeners(this);
    }
}
5
  • Can you please confirm additional details such as: 1. are you using the sample pub/sub code that Salesforce provided in the lwc samples; 2. are both components in the same lightning page; 3. are both components in the same namespace?
    – Phil W
    Mar 30, 2020 at 22:03
  • (Not showing the imports makes it harder to help you and omitting the pub/sub code detail does too)
    – Phil W
    Mar 30, 2020 at 22:04
  • hi @PhilW yes i am using the same pubusb and component is in the same lightning page Apr 1, 2020 at 1:55
  • Same namespace?
    – Phil W
    Apr 1, 2020 at 6:37
  • And please edit the question to add the imports used.
    – Phil W
    Apr 1, 2020 at 6:38

2 Answers 2

1

Try to import lightning/navigation and pubsub in your JS

import { NavigationMixin, CurrentPageReference } from 'lightning/navigation';
import { fireEvent } from 'c/pubsub';

JS method

export default class RealtedListTable extends NavigationMixin(LightningElement) {
2
0

Ok I was able to get fire the event by doing the following

     showRelatedList(event){
        let buttonName = event.target.name;
        let accId = this.account.Id;
        console.log('Account Id',accId);
        console.log('Button Name',buttonName);
        if(buttonName ==='contact' || buttonName ==='Opportunity'){
            //alert('Button Name '+buttonName);
           /* let apexParam = {
                "aId" : accId,
                "btName" : buttonname
            };
            console.log('This is the apex param ',apexParam);
            alert(apexParam);
            fireEvent(this.pageRef, 'relatedlist' , apexParam); */
            this.getRelatedList(accId,buttonName);
            
        }
        
    }

    getRelatedList(accountId,botName){
        console.log('This is Accont Id from ',accountId);
        let apexParam = {
            "aId" : accountId,
            "btName" : botName
        };
        console.log('This is the apex param ',apexParam);
        alert(apexParam);
        fireEvent(this.pageRef, 'relatedlist' , apexParam);

    }

You must log in to answer this question.

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