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!