Hello friends, In this post i am going to post a batch apex example that helps you to read csv file and insert data dynamically in any salesforce object. so let's get started.
Read and insert csv file data using batch apex
Note: Before proceeding to this post, you must have to complete the below post. Because I referenced the class that is created through the below post.
I used the following csv file to read and insert data using batch apex. Simply upload this file in document and used in my batch apex class. Please make sure csv file column name is similar to salesforce object field's api name.
To read above file we have to create an batch apex class and replace the following code.
CSVImportBatchJob.apxc
global class CSVImportBatchJob implements Database.Batchable<SObject>, Schedulable {To execute this code manually, Navigate to Developer Console | Debug | Open Execute Anonymous Window and execute the following code.
global Database.QueryLocator start(Database.BatchableContext context) {
return Database.getQueryLocator('SELECT Id, Name, Body FROM Document WHERE Name = \'DemoCSV\'');
}
global void execute(Database.BatchableContext context, List<SObject> records) {
// Cast the first record as a Document object
Document theDocument = (Document)records.get(0);
System.debug('theDocument: ' + theDocument);
// Read the document's body as CSV records
List<List<String>> csvRecords = CSVReader.doParse(theDocument.Body);
System.debug('csvRecords: ' + csvRecords);
import(csvRecords, 'Account');
}
global void finish(Database.BatchableContext context) {
}
global void execute(SchedulableContext context) {
Database.executeBatch(this, 1);
}
global static void import(List<List<String>> csvRecords, String objectName) {
List<String> fieldNames = csvRecords.remove(0);
import(csvRecords, objectName, fieldNames);
}
global static void import(List<List<String>> csvRecords, String objectName, List<String> fieldNames) {
SObjectType objectType =Schema.getGlobalDescribe().get(objectName);
import(csvRecords, objectType, fieldNames);
}
global static void import(List<List<String>> csvRecords, SObjectType objectType, List<String> fieldNames) {
System.debug('csvRecords: ' + csvRecords);
System.debug('objectType: ' + objectType);
System.debug('fieldNames: ' + fieldNames);
// Initialize the list of records to insert
List<SObject> newRecords = new List<SObject>();
// Read each row and parse the row as value to insert
while (csvRecords.size() > 0) {
// Initialize the new record
SObject newRecord = objectType.newSObject();
// Read the CSV values
List<String> values = csvRecords.remove(0);
// Read each value, matching it up to the correct field
// based on the position of the value in the row
if (values.size() > 0) {
for (Integer i = 0; i < fieldNames.size(); i++) {
String fieldName = fieldNames.get(i);
String fieldValue = values.get(i);
newRecord.put(fieldName, fieldValue);
}
newRecords.add(newRecord);
}
}
insert newRecords;
}
}
Database.executeBatch(new CSVImportBatchJob(), 1);
Output:
Hope you like this post, for any feedback or suggestions please feel free to comment. I would appreciate your feedback and suggestions.
Thank you.
0 Comments
Post a Comment