+ Start a Discussion
JonathanFox UKJonathanFox UK 

LWC not calling apex method

My LWC calls a batch job class which inserts contacts. This works fine, however I am now trying to display a progress bar or spinner to show the status of the batch job.

I am trying to pass the Job Id into an Apex method which queries and returns the status.

As I want this fluid, it needs to 'refresh' and call frequently until the job is done in order to show the progress bar.

My Method is failing to execute. -> getBatchJobStatus

I also occasionally get a [Violation] 'setInterval' handler took xx ms.

Below is my .js
 
import { LightningElement, track, wire } from 'lwc';
import initiateContactBatch from '@salesforce/apex/BatchCreateContactController.initiateContactBatch';
import getBatchJobStatus from '@salesforce/apex/BatchCreateContactController.getBatchJobStatus';
export default class BombBurstContacts extends LightningElement {

    

    @track numberInput = 0;
    @track jobID = null;
    @track createError;
    @track error;
    @track record;
    @track processing = false;
    @track jobPercentage;
    @track totalJobItems;
    @track jobItemsProcessed;
    @track status;
    @track progress = 500;  
  
    onChange(event){
        this.numberInput = event.target.value;
        console.log('this.numberInput' + this.numberInput);
    }
    
    createRecords(){
        
        initiateContactBatch({ numberOfRecords : this.numberInput })
            .then(result => {
                this.jobID = result;
                this.processing = true;
                console.log('this.jobID' + this.jobID);
                console.log('this.processing' + this.processing);

                this.connectedCallback();
                
            })
            .catch(error => {
                this.createError = error;
                console.log('this.createError' + this.createError);
                
            });
    }

    connectedCallback() {  
        if(this.jobID != null){
            this._interval = setInterval(() => {  
                
                
               //this.getStatus();

               this.getBatchJobStatus({ jobID : this.jobID})
                .then(result => {
                    this.record = result;
                    console.log('this.record' + this.record);
                })
                .catch(error => {
                    this.error = error;
                    console.log('this.error' + this.error);
                });
                
                this.progress = this.progress + 25000;
                console.log('this.progress' + this.progress);
                
                if ( this.progress === 200000 ) {  
                    clearInterval(this._interval);  
                }  
            }, this.progress);  
        }
  
    }  

    // getStatus(){
    //     getBatchJobStatus({ jobID : jobID})
    //     .then(result => {
    //         this.record = result;
    //         console.log('this.record' + this.record);
    //     })
    //     .catch(error => {
    //         this.error = error;
    //         console.log('this.error' + this.error);
    //     });
    // }


    
    
}

 
SwethaSwetha (Salesforce Developers) 
HI Jonathan,

I see your question is also posted on https://salesforce.stackexchange.com/questions/318003/lwc-failing-to-call-apex-class-method wherein you were suggested "to create a separate progress bar component (with a refresh icon button) and use it your code. Refer to the sample shown in the documentation section here: https://developer.salesforce.com/docs/component-library/bundle/lightning-progress-bar/documentation. This child component could dispatch a custom event on click of the refresh button (or automatically after certain interval) to the parent component (asking to fetch the latest job status) and the parent component could send the information to a public method in the child component."

If the said approach works, please close this thread by marking the answer as best so that other facing the same issue will find this info helpful. Thanks