You need to sign in to do that
Don't have an account?
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.
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.
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
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];
}
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
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)
{
}
query error like bloew
System.QueryException: List has no rows for assignment to SObject
Use this code in the start method.
//Start Method
global List<Account> start(Database.BatchableContext BC)
{
if(!lstAccounts.isEmpty())
{
return lstAccounts;
}else{
return null;
}
}
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