I have csv file with account records and want to insert that records via apex. How can i do it and dynamically find necessary fields? I can only manually assign them and its hard to parse cause of their name in csv file (Account ID,Account Name,Account Type). I dont want to write : a.Name = inputvalues[1]; I want to retrive field-data from csv somehow.
So now it looks like that, but i want it to be more reusable
public static Account[] getAccountData(){
StaticResource sr = [SELECT Id, Body FROM StaticResource WHERE Name = 'Account' LIMIT 1];
String body = sr.Body.toString();
String[] filelines = body.split('\n');
String n = 'Account';
List<Account>accs = new List<Account>();
for (Integer i=1;i<filelines.size();i++)
{
String[] inputvalues = new String[]{};
inputvalues = filelines[i].split(',');
System.debug(inputvalues);
Account a = new Account();
a.Name = inputvalues[1];
a.Type = inputvalues[2];
accs.add(a);
}
return accs;
}
csv:
Account ID,Account Name,Account Type
"0010900000oJcmQAAS","Sample Account for Entitlements",""
"0010900000pOqyQAAS","Edge Communications","Customer - Direct"
"0010900000pOqyRAAS","Burlington Textiles Corp of America","Customer - Direct"
"0010900000pOqySAAS","Pyramid Construction Inc.","Customer - Channel"