Apex returns the Same Result when called through SetInterval

Viewed 27

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'}

1 Answers

No promises it'll work but here are relevant, anonymised bits of my old progress bar. It refreshes every 2 seconds

html

<template>
      Status: {job.Status}
      <lightning-progress-bar value={progress}></lightning-progress-bar>

      <lightning-button variant="success" label="Process" icon-name="utility:process" title="(Re) run the job"
          onclick={handleProcess}>
      </lightning-button>
</template>

js

import { LightningElement, wire } from 'lwc';
import getJobStatus from '@salesforce/apex/SomeClass.getJobStatus';
import processData from '@salesforce/apex/SomeClass.processData';

export default class someComponent extends LightningElement {
    progress = 0;
    job = {};
    timeout;

    handleProcess(event) {
        processData()
            .then(result => {
                this.job = result;
                this.updateProgress();
            });
    }

    updateProgress() {
        this.progress = Math.round((this.job.JobItemsProcessed * 100) / this.job.TotalJobItems);
        if (this.job.Status == 'Completed') {
            clearTimeout(this.timeout);
        } else {
            this.timeout = setTimeout(() => {
                getJobStatus({ i: this.job.Id })
                    .then(result => {
                        this.job = result;
                        this.updateProgress();
                    });
            }, 2000);
        }
    }
}

apex

public with sharing class SomeClass{

    @AuraEnabled
    public static AsyncApexJob processData(){
        return getJobStatus(Database.executeBatch(new SomeBatch()));
    }

    @AuraEnabled
    public static AsyncApexJob getJobStatus(Id i){
        return [SELECT Id, Status, JobItemsProcessed, TotalJobItems
            FROM AsyncApexJob
            WHERE Id = :i];
    }
}
Related