+ Start a Discussion
Raksha JhaRaksha Jha 

LMS with LWC - Datatable

How to send notification via LMS on change of data in LWC - Datatable?
I have created a leaderboard which display the scores of winning player through LWC- Datatable. i have to publish message through LMS whenever winning player change.
This is Apex class:
public with sharing class SquadLeaderBoardHandler {
	@AuraEnabled(cacheable=true)
    public static List<Account> getUpdatedScore() 
    {
        Contact con = [SELECT AccountId FROM Contact WHERE Email = :UserInfo.getUserEmail()];

        Account objAccount = new Account();
        objAccount = [SELECT Name, Squad__r.Name, Id FROM Account WHERE Id = :con.AccountId];
        //WHERE Id = :objUser.Id


        return [SELECT Name, Weekly_Arete_Number__c, Squad__r.Name FROM Account 
        WHERE Squad__r.Name = :objAccount.Squad__r.Name ORDER BY Weekly_Arete_Number__c DESC LIMIT 3];
    }
}
Leaderboard.html:
<template>
	<lightning-card title={squadName} onchange={publishMessage} icon-name="utility:activity">		
			<div if:true={accounts}>
				<lightning-datatable data={accounts}
					columns={columns}
					key-field="id"
					hide-checkbox-column="true">
				</lightning-datatable>
			</div>		
</lightning-card>
</template>

LeaderBoard.js
import { LightningElement, wire, track } from 'lwc';
import getUpdatedScore from '@salesforce/apex/SquadLeaderBoardHandler.getUpdatedScore';
import LBMC from '@salesforce/messageChannel/LeaderBoardMessageChannel__c';
import { MessageContext, publish } from 'lightning/messageService';

// datatable columns
const COLUMNS = [
    { label: 'Hero Name', fieldName: 'Name', hideDefaultActions: true, type: 'text' },
    { label: 'Hero Score', fieldName: 'Weekly_Arete_Number__c', hideDefaultActions: true, type: 'Number' },
];

export default class SquadLeaderboard extends LightningElement {
    @track squadName;
    @track accounts;
    @track columns = COLUMNS;
    @track error;

    @wire(getUpdatedScore)
    wiredupdatedScore({ error, data }) {
        if (data) {
            this.accounts = data;
            this.error = undefined;
            this.squadName = 'Squad Leaderboard for: ' + this.accounts[0].Squad__r.Name;
            console.log(this.accounts);

        } else if (error) {
            this.error = error;
            this.accounts = undefined;
        }
    }

    @wire(MessageContext)
    context;
    publishMessage(event) {
        this.squadName = event.target.value;
        console.log('SquadName: ' + this.squadName);
        const message = { slbData: { value: this.squadName } };
        publish(this.context, LBMC, message);

    }

}
message channel
<?xml version="1.0" encoding="UTF-8" ?>
<LightningMessageChannel xmlns="http://soap.sforce.com/2006/04/metadata">
    <masterLabel>LeaderBoardMessageChannel</masterLabel>
    <isExposed>true</isExposed>
    <description>Message Channel to pass data</description>
   	<lightningMessageFields>
			 <fieldName>slbData</fieldName>
        <description>This is the field to pass SquadLeaderBoard Data</description>
		</lightningMessageFields>
		<lightningMessageFields>
			<fieldName>clbData</fieldName>
			<description>This is the field to pass CohortLeaderBoard Data</description>
		</lightningMessageFields>
		<lightningMessageFields>
			<fieldName>tlbData</fieldName>
			<description>This is the field to pass TeamLeaderBoard Data</description>
    </lightningMessageFields>
	</LightningMessageChannel>

Thanks!

 
MagulanDuraipandianMagulanDuraipandian
Raksha,
LMS is for passing data between the components. You can try using PushTopic and emApi.
Sample Code: https://www.infallibletechie.com/2020/03/subscribe-and-show-pushtopic-events.html
Raksha JhaRaksha Jha
Thank you for your response. I have to publish the winner from leaderboard so that other components subscribe and a notification will display with winner name using LMS.