function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
RoyalRoyal 

batch class to read csv file then create a Account ?

HI,
i have CSV file in documents, that have to read from BATCH class then create Account..

1. how to read CSV from Documents ?
2. how to return query from start method ?

can any one give me sameple code for this.

Thanks in advance.

 
Best Answer chosen by Royal
suresh sanneboina 4suresh sanneboina 4
global class BatchcreateAccounts implements Database.Batchable<sObject>

    List<Account> lstAccounts=new List<Account>();
    global BatchcreateAccounts()
    {
    Document doc=[SELECT Body,ContentType,Description,DeveloperName,Name FROM Document WHERE Name = 'Csv File'];
    String[] columns=doc.Body.toString().split('\n');
    String[] columnNames=columns[0].split(',');
    columns.remove(0);

    for(String str:columns)
    {
        Account acc=new Account();
        String[] fields=str.split(',');
        for(Integer i=0;i<fields.size()-1;i++)
        {
            acc.put(columnNames[i],fields[i]);
        }
        lstAccounts.add(acc);
    }
    }

    //Start Method
    global List<Account> start(Database.BatchableContext BC)
    {
        return lstAccounts;
    }
    
    //execute method
    global void execute(Database.BatchableContext BC, List<Account> scope)
    {    
        
    }
    
    //Finish method
    global void finish(Database.BatchableContext BC)
    {

    }

All Answers

suresh sanneboina 4suresh sanneboina 4
Hi Please try the below example
Document doc=[SELECT Body,ContentType,Description,DeveloperName,Name FROM Document WHERE Name = 'Csv File'];
String[] columns=doc.Body.toString().split('\n');
String[] columnNames=columns[0].split(',');
columns.remove(0);
List<Sobject> lstAccounts=new List<Sobject>();
for(String str:columns)
{
    Account acc=new Account();
    String[] fields=str.split(',');
    for(Integer i=0;i<fields.size()-1;i++)
    {
        acc.put(columnNames[i],fields[i]);
    }
    lstAccounts.add(acc);
}

process this list(lstAccounts) from start method in batch class.in the execute method try to insert the record
if(!lstAccounts.isEmpty())
{
    insert lstAccounts[0];
}
RoyalRoyal
Hi Suresh,

i have tried but in start method return type is Database.QueryLocator but "lstAccounts" is list of Sobject so throwing error like below..
Error: Compile Error: Return value must be of type: Database.QueryLocator at line 51 column 4

 
suresh sanneboina 4suresh sanneboina 4
global class BatchcreateAccounts implements Database.Batchable<sObject>

    List<Account> lstAccounts=new List<Account>();
    global BatchcreateAccounts()
    {
    Document doc=[SELECT Body,ContentType,Description,DeveloperName,Name FROM Document WHERE Name = 'Csv File'];
    String[] columns=doc.Body.toString().split('\n');
    String[] columnNames=columns[0].split(',');
    columns.remove(0);

    for(String str:columns)
    {
        Account acc=new Account();
        String[] fields=str.split(',');
        for(Integer i=0;i<fields.size()-1;i++)
        {
            acc.put(columnNames[i],fields[i]);
        }
        lstAccounts.add(acc);
    }
    }

    //Start Method
    global List<Account> start(Database.BatchableContext BC)
    {
        return lstAccounts;
    }
    
    //execute method
    global void execute(Database.BatchableContext BC, List<Account> scope)
    {    
        
    }
    
    //Finish method
    global void finish(Database.BatchableContext BC)
    {

    }
This was selected as the best answer
RoyalRoyal
Hi Suresh,
 query error like bloew

System.QueryException: List has no rows for assignment to SObject
suresh sanneboina 4suresh sanneboina 4
that list has no data. Please check.
Use this code in the start method.
   //Start Method
    global List<Account> start(Database.BatchableContext BC)
    {
        if(!lstAccounts.isEmpty())
        {    
            return lstAccounts;
        }else{
            return null;
        }
        
    }
RoyalRoyal
Thanks A lot Suresh, it's working, i given file name wrong, now it's working fine. thanks Once again
RoyalRoyal
Hi Suresh,

the same senario how can read CSV file from local system.

like file keep in" D:\CSV\File name " location in local system at this time how to write code to read CSV file.

Thanks



 
suresh sanneboina 4suresh sanneboina 4
Using automated dataloader or CLI(Command line interface) you can achieve this.