0

I have requirement where user have to process multiple records each at a time due to governor limitations.

For the incoming multiple records each record is taken and calls apex. After the successful response have to place the same request again. Like so to process all records.

Can someone help me how to achieve this.

import { LightningElement, api, wire, track } from 'lwc';
import createRecord from '@salesforce/apex/OpportunityLoadController.createRecord';
export default class OpportunityLoadLWC extends LightningElement {  
@api error;
@api resultLst = [];
@api message; 
//
//Other logic
// 
handleClick(e) {
        var oneRecord = resultLst.pop();
        createRecord({
           data : oneRecord
        })
        .then((result) => {
            //call createRecord again
        })
        .catch((error) => {
            this.message = 'Error received: code' + error.errorCode + ', ' +
                'message ' + error.body.message;
        });
    }

}
naveen
  • 159
  • 2
  • 8
  • Why not make the calls in parallel, or use an Apex batch class? Processing records one at a time is terribly inefficient, and generally not a good implementation pattern. – David Reed Jul 23 '19 at 04:00
  • The reason is have to take response for each record and show parallel to user. – naveen Jul 23 '19 at 04:09

1 Answers1

0

It would not be optimized if we call apex for one record each

However, it is possible to get a use case where you have thousands of records which need to be inserted, or the above use case.

This can be achieved by chunkifying the records and placing them in a queue,

You might need to use setInterval() to keep checking whether the queue has a record and needs to be processed.

Use the queue with async/await to process one chunk at once and voila!

Again, the solution is resource intensive and should be used if that's not a concern.