I am trying to implement a progress bar indicator for APEX Jobs that are running in the background, however when I call the APEX method through the setInterval method, it is returning the same values again, and the values are not getting updated. Every 4 Seconds it gives the same output.
import { LightningElement, wire,api } from 'lwc';
import {subscribe, MessageContext} from 'lightning/messageService';
import recordSelected from '@salesforce/messageChannel/unrelatedcomponent__c';
import getJobDetails from '@salesforce/apex/GetJobDetailForProgressbar.getJobDetails';
export default class Progressbarlms extends LightningElement {
subscription = null;
// @api result = [];
@api progress = 0;
@api progressresult;
@api c = 10;
@api batchidd = '';
@wire(MessageContext)
selectedrecord;
connectedCallback(){
this.subscribeToMessageChannel();
}
subscribeToMessageChannel(){
this.subscription = subscribe(
this.selectedrecord,
recordSelected,
(record)=>this.handleRecordSelected(record)
);
console.log('Inside unrelatedcomponent');
}
handleRecordSelected(record){
this.batchidd = record.Batchid;
console.log('Inside unrelatedcomponent selectedRecords', this.batchidd);
if(this.c==10){
console.log('inside if statement');
this._interval = setInterval(()=>{this.getprogress()}, 4000);
}
}
getprogress(){
getJobDetails({Jobid: this.batchidd}).then((result) => {
console.log(result);
});
}
}
Output : data {JobItemsProcessed: 8, TotalJobItems: 300, Id: '7079I000001e13FQAQ'}
public class GetJobDetailForProgressbar {
@AuraEnabled(cacheable=true)
public static sObject getJobDetails(String Jobid){
sObject jobDetail = [SELECT JobItemsProcessed, TotalJobItems FROM AsyncApexJob WHERE ID =: Jobid];
// SELECT Status, NumberOfErrors,JobItemsProcessed,TotalJobItems FROM AsyncApexJob WHERE Id = :jobID
return jobDetail;
}
}
I've tried wiring the apex method and using refresh apex, but when I wire the Apex method, it only return the output as : data {JobItemsProcessed: 0, TotalJobItems: 0, Id: '7079I000001e13FQAQ'}
@AuraEnabled, should be better. I'll try to dig up my progress bar thing later this evening