1

Good day everyone,

Maybe this question is basic for everyone but I'm having a problem with it, so I'm asking for help. I have created a BATCH ITERABLE .

global with sharing class batchProcessor implements Database.batchable<String>, Database.Stateful
{
global Iterable<String> start(Database.batchableContext batchableContext)
   { 
     // m_csvFile is blob data in csv file
       return new CSVIterator(m_csvFile.toString(), Parser.crlf);
   }
   global void execute(Database.BatchableContext batchableContext, List<String> scope)  
   {
      // I want to get the first row of the csv or the header for mapping
      System.debug(scope[0]);
   }
}

This is also my CSVIterator.class

global with sharing class CSVIterator implements Iterator<String>, Iterable<String>
{
   private String m_CSVData;
   private String m_introValue;
   public CSVIterator(String fileData, String introValue)
   {
      m_CSVData = fileData;
      m_introValue = introValue; 
   }
   global Boolean hasNext()
   {
      return m_CSVData.length() > 1 ? true : false;
   }
   global String next()
   {
      String row = m_CSVData.subString(0, m_CSVData.indexOf(m_introValue));
      m_CSVData = m_CSVData.subString(m_CSVData.indexOf(m_introValue) + m_introValue.length(),m_CSVData.length());
      return row;
   }
   global Iterator<String> Iterator()
   {
      return this;   
   }
}

What i want to get at the moment is the first row of the iterable which is the csv header. As you've seen in my batch class i try to System.debug the scope[0] but the result is not the csv header ? How will i do it ?

Pls help.

Thank you !!!

1

The execute method will be called multiple times with the next batch of rows so you need to grab the first row on the first call to the execute. Because you have the marker interface Database.Stateful, any fields you add to the class will then be preserved between batches:

public with sharing class batchProcessor implements
        Database.Batchable<String>, Database.Stateful {
    ...
    // Will be preserved between execute calls
    private String[] headings;
    ...
    public void execute(Database.BatchableContext bc, List<String> scope) {
        // If variable is null it's the first batch
        if (headings == null) {
            headings = scope[0].split(',');
        }
        ....
    }
    ...
}

Note that the code no longer needs to be global and public is usually the appropriate choice.

7
  • What a very simple answer for the question that give give me headache. Thank you so much. I've tried it and its working. You're the man.
    – Hope
    Jul 15 '15 at 13:30
  • By the way what if i tried to check if I am in the first row of the next batch. I've tried to add another variable and then add an else statement where i initialize that variable but it is not working. I also want this because I only want to escape the first row of the first batch and nothing more. Pls help. Thanks...
    – Hope
    Jul 16 '15 at 4:17
  • @Hope Your batchable should work with varying batch sizes so doing something on the first row of the next batch seems wrong to me. But maybe I'm misunderstanding what you are trying to do. Probably best if you post a new question with your current code.
    – Keith C
    Jul 16 '15 at 7:43
  • Sorry to bother you.But I encounter some problem with this one. I've tried to read 220 rows of csv and what I've notice is that when there is an error in the first 200 which the default number of batch, the header will get data from row 201.Why is that ? please help.
    – Hope
    Sep 25 '15 at 2:59
  • @Hope Do you mean that row 201 is used as the header for the second batch? If so I can imagine that the first batch failing doesn't result in the state information of the class - its headings field - being preserved as the transaction for the first batch is rolled back. But I can't think of a solution - I suggest you post a new question about that scenario to see if anyone has a solution.
    – Keith C
    Sep 25 '15 at 8:25

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.