0

I have below Javascript function in lwc

apexJobInfoData;
@track isBatch;
getData({ error, data }) {
    if (error) {
        // TODO: handle error
    } else if (data) {
        this.jobId = getFieldValue(data, JOB_ID);
        getAllApexJobInfo({ ApexJobId: this.jobId})
        .then(result => {
            this.apexJobInfoData = result.JobInfo.length===0?null:result.JobInfo;
            this.error = undefined;

            if(apexJobInfoData!=null){
                apexJobInfoData.forEach((element) => {
                        if(element.JobType === 'BatchApex'){
                            this.isBatch = true;    
                        }
                  })
            }
            alert('Error Calling Alert Here');  // This line does not execute
        })
        .catch(error => {
            this.message = undefined;
            this.error = error;
        });
    }

I am not sure what's happening but after there is something wrong with below piece of code. After this nothing gets executed.

        if(apexJobInfoData!=null){
            console.log('Enters here');
            apexJobInfoData.forEach((element) => {
                 console.log('Doesn't enter here');
              })
        }

What am I missing here.

6
  • Can you put a screenshot of the result of apexJobInfoData after logging it via console.log. Maybe it is not an Array but a String that looks like Array. Also, can you add console logs to know exactly at what line does it stop.
    – manjit5190
    Commented Mar 17, 2020 at 16:22
  • I have added a console log. It does enter into If loop but there is something wrong with how I am using forEach.
    – SfdcBat
    Commented Mar 17, 2020 at 16:24
  • This probably fix your problem, but you could write this.isBatch = !!apexJobInfoData.find(element=>element.JobType === 'BatchApex') instead.
    – sfdcfox
    Commented Mar 17, 2020 at 16:25
  • It can be that apexJobInfoData is not an Array but a String like '[a,b,c,d]'
    – manjit5190
    Commented Mar 17, 2020 at 16:26
  • [ { "Id": "7072900001Sf8CmXXXX", "JobType": "Future", "Status": "Failed", "JobItemsProcessed": 0, "TotalJobItems": 0, "NumberOfErrors": 0, "CompletedDate": "2020-03-11T19:15:32.000Z", "MethodName": "XX", "ExtendedStatus": "XXXX" } ]
    – SfdcBat
    Commented Mar 17, 2020 at 16:26

1 Answer 1

3

Looks to me like you are missing "this" references:

    .then(result => {
        this.apexJobInfoData = result.JobInfo.length===0?null:result.JobInfo;
        this.error = undefined;

        if(this.apexJobInfoData!=null){ // HERE
            this.apexJobInfoData.forEach((element) => { // HERE TOO
                    if(element.JobType === 'BatchApex'){
                        this.isBatch = true;    
                    }
              })
        }
2
  • Is there a way to catch these kind of mistakes in JS?
    – SfdcBat
    Commented Mar 17, 2020 at 16:40
  • You might find that jslint, eslint or other code checker can help.
    – Phil W
    Commented Mar 17, 2020 at 17:05

You must log in to answer this question.

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