Please try the below example. Create a csv file on documents and use the below code to create records.
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();i++) { acc.put(columnNames[i].trim(),fields[i]); } lstAccounts.add(acc); } if(!lstAccounts.isEmpty()) { insert lstAccounts[0]; }
No comments :
Post a Comment