0

I have a trigger that stores data, the data is then displayed on a lightning data-table when the web page is first loaded. I have @wired the logList function but data-table doesn't refresh and update when new data is stored. Is there a way to use refreshApex in a Trigger to get real-time updates?

js:

import { LightningElement, wire, track } from 'lwc';
import getLog from '@salesforce/apex/FridgeLogsController.getLog';

const columns = [
    {label: 'Timestamp', fieldName: 'LastModifiedDate', type: 'date',
        typeAttributes:{
            weekday: "long",
            year: "numeric",
            month: "long",
            day: "2-digit",
            hour: "2-digit",
            minute: "2-digit"
        }
    },
    {label: 'Employee Name', fieldName: 'employeeName__c', type: 'text'},
    {label: 'Fridge ID', fieldName: 'fridgeId__c', type: 'text'},
    {label: 'Phone #', fieldName: 'contactPhone', type: 'phone'},
];

export default class ExerciseTrigger extends LightningElement {
    columns = columns;
    refreshTable;
    @track log;

    @wire(getLog) logList(result) {
        if (result.data) {
            this.refreshTable = result;
            this.error = undefined;

            let preparedArr = [];
            result.data.forEach(record => {
                let preparedRec = {};
                preparedRec.Id = record.Id;
                preparedRec.date = record.LastModifiedDate;
                preparedRec.LastModifiedDate = record.LastModifiedDate;
                preparedRec.employeeName__c = record.employeeName__c;
                preparedRec.fridgeId__c = record.fridgeId__c;
                preparedRec.contactPhone = record.Contact__r.Phone;

                preparedArr.push(preparedRec);
            });
            this.log = [...preparedArr];
            
        } else if (result.data) {
            this.log = result.error;
            this.error = undefined;
        } 
    }
}

Trigger:

trigger FridgeTrigger on Fridge_Log__e (after insert) {
    // List to insert
    list<Fridge_Logs__c> dataToInsert = new List<Fridge_Logs__c>();
    
    for (Fridge_Log__e event : Trigger.New) {
        System.debug('Data ID from emitter: ' + event.badgeId__c);
        // Record to add
        Fridge_Logs__c data = new Fridge_Logs__c();
        // Store event into variable for storing
        data.badgeId__c = event.badgeId__c;
        data.employeeName__c = event.employeeName__c;
        data.fridgeId__c = event.fridgeId__c;
        List<Contact> lookupList = [SELECT Id FROM Contact WHERE Name = :data.employeeName__c ];
        data.Contact__c = lookupList[0].Id;
        dataToInsert.add(data);
        
        if(!dataToInsert.isEmpty()){
            // Record data in db
            insert dataToInsert;
        } 
    }
}

1 Answer 1

1

Not, you can't call refreshApex from Apex. Instead, you can use lightning-emp-api to listen for Platform Events, which you can use EventBus.publish to fire notifications from your trigger.

You must log in to answer this question.

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