0

I'm working on a LWC component which will work as a simple snap-in to do basic data uploads for very non technical users.

My issue is that I want to get the SaveResults from a batch, and pass it into the LWC.

I've set up a component which can grab a file, parse it and start a batch upload. To keep track of the upload status I've implemented a loading bar, which will query the server for the Job status and amount of records processed in order to calculate the upload progress.

The batch is run in Stateful context so that I can aggregate the saveresults, which I want to pass back into the LWC GUI once the batch upload is finished.

If I wouldn't go by Batch I could just do a DML, and return the list.

public static List<Database.SaveResult> doUpload(data){
     //Do some work  
     Database.saveResult[] srList= Database.inset(data,false);

return srList;

}

However, since I do it by batch I cannot just have the saveresults as a return type, is there any other way to get this data out?

The reasonI do it by batch is since the volumes can become pretty big, and I want to be able to have a progressbar.

APEX which is called from the LWC

        databaseOperationBatch b = new databaseOperationBatch(dataToInsert);
        batchJobID = Database.executeBatch(b,batchSize);

        //LWC uses batchJobID to check upload status.
        return batchJobID;

And the batch would look something like this


global class databaseOperationBatch implements Database.Batchable<List<String>>, Database.Stateful {

    public map<String,List<string>> resultMap = new map <String,List<String>>();

 global Iterable<data> start (){
   return scope;
 }

 global void execute(){
            //Do some work
            Database.SavevResult srList = Database.insert(data,allOrNone);
            for(Database.SaveResult sr : srList){
                if(sr.isSuccess()){
                    successList.add(sr.getId());
                    system.debug('inserted: ' + sr.getId());
                }
                else{
                    for(Database.Error err : sr.getErrors()){
                        system.debug(err.getStatusCode() + ' : ' + err.getMessage());
                        failureList.add(err.getStatusCode() + ' : ' + err.getMessage());
                    }
                }
            }
            resultMap.put(successList);
            resultMap.put(failureList);
        }

 global void finish(){
   //Can i return the resultmap here somehow, or query for it?
 }
}

Any pointer to get me in the right direction would be really helpful - Or just to know if it's a futile effort and i should skip the batch approach entirely.

Thanks!

1

You can use platform event or Generic event ( which will be user specific)' here for your use case.

Subscribe to Platform event in your LWC component.

https://newstechnologystuff.com/2020/08/15/platform-event-in-lightning-web-components/

Then in the finish method of your batch just publish the platform event notifying your UI that the batch job is finished.

https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_publish_apex.htm

This should work.

3
  • Thanks for the reply Nagendra! I hadn't thought about events.. So for passing the save results to the LWC - I would publish an event for each save result? Because from what i can see it is not possible to pass a map in the payload of the event? Feels like passing one event for each result would end up eating up alot of of the allocated events limit.. – Cnork Nov 20 '20 at 13:15
  • No need for publishing for each save result. Think how you can bulkify, a suggestion - Keep a stateful param (Map or List) at top, keep adding your data to it , Just convert map/List to json string, store it in Platform event field (Text area), like serialize it and pass to ui and using parse method from javascript convert it back to map or object – Nagendra Singh Nov 20 '20 at 13:25
  • Ah yes ofc! A JSON is the wat to go. Thanks alot Nagendra, this is going to solve it :) – Cnork Nov 20 '20 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.