0

I have a class that is parsing a csv file but running into heap limits. To resolve that I am converting it into a batchable class. However the List of lists is not getting accepted. I am getting the error

'Class CSVBatchReader must implement the method: void Database.Batchable.execute(Database.BatchableContext, List) (1:14)'

The original class that worked with a small csv file..(the intention is to insert the records so the two loops are just to see the data)

public with sharing class ReadCSVResource {
    public static void readCSVResource() {
        StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'Courses' LIMIT 1];
        String body = sr.Body.toString();
        Blob csvBlob = Blob.valueOf(body);
        List<List<String>> rcvdVal = CsvReader.readIETFRFC4180CSVFile(csvBlob);

        List<String> flabels = rcvdVal[0];
        List<String> values = rcvdVal[1];
        for(Integer i = 0; i<flabels.size(); i++)
        {
            System.debug('Field ' + i + ' ' + flabels[i]);
        }
        for(Integer k = 0; k< values.size(); k++){
            System.debug('Field ' + k + ' ' + values[k]);
        }
        
    }
}

After trying to convert to a batchable class..

public class CSVBatchReader implements Database.Batchable<sObject>, Database.Stateful {
       
    public Database.QueryLocator start(Database.BatchableContext bc) {
        StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'Courses' LIMIT 1];
        String body = sr.Body.toString();
        Blob csvBlob = Blob.valueOf(body);
        List<List<String>> rcvdVal = CsvReader.readIETFRFC4180CSVFile(csvBlob);
        return rcvdVal;       
        
    }
    public void execute(Database.BatchableContext bc, List<List<String>> rcvdVal){
        // process each batch of records
        
        List<String> flabels = rcvdVal[0];
        List<String> values = rcvdVal[1];
        for(Integer i = 0; i<flabels.size(); i++)
        {
            System.debug('Field ' + i + ' ' + flabels[i]);
        }
        for(Integer k = 0; k< values.size(); k++){
            System.debug('Field ' + k + ' ' + values[k]);
        }
    }
    public void finish(Database.BatchableContext bc){
        System.debug(' Records processed. Shazam!');
       
    }
}

How can I rewrite this to allow my csv not to run to heap limit?(size is 1.9MB)

Thanks!

UPDATE - WHAT WORKED

public Iterable<List<String>> start(Database.BatchableContext bc)  {
        StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'Courses' LIMIT 1];
        String body = sr.Body.toString();
        Blob csvBlob = Blob.valueOf(body);
        List<List<String>> rcvdVal = CsvReader.readIETFRFC4180CSVFile(csvBlob);
        return rcvdVal;       
        
    }
1
  • 1
    Have you tried using bulk api ?
    – MIX DML
    Commented Aug 27, 2022 at 10:19

1 Answer 1

0

You have data type problems in your code.

First, the interface must match the data you're returning. Here, it's a List<List<String>>, so your interface needs to reflect that by:

public class CSVBatchReader implements Database.Batchable<List<String>> {

As a side note, I don't see any reason to use Database.Stateful here, as you have no state that you're storing.

Second, the start method's return type isn't correct, you'd have to use something like:

public Iterable<List<String>> start(Database.BatchableContext bc) {

You only specify a return type of Database.QueryLocator when you're using Database.getQueryLocator(query).

The compiler error you received from the execute method was an indication that your data types from the two previous steps did not agree with the data type for the parameter in the execute method.

1
  • Thank you so much! I updated to reflect what worked.
    – Pauline
    Commented Aug 27, 2022 at 17:14

You must log in to answer this question.

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