Apex · Salesforce

CSV parsing to sObjects

Suppose there is a requirement to create records from a CSV file without using Data Loader. How it can be achieved ? An apex utility class is used that can read the CSV files row by row and create the required object records.

How this approach is prior to Data Loader ?

Using apex utility class over Data Loader gives more flexibility. Data Loader cannot be scheduled. Multiple CSV files at a time will not work in Data Loader. Mappings are not saved for later similar uploads. One can do all the above processes using Apex utility class.

Step by step process to create this tool

To make this utility tool more reusable, first create two objects named File Format with no custom fields and Upload File Column which is a child of File Format. Upload File Column object has fields as Column Name(text), Column Field Name(picklist) and Column Number(number). Next a custom setting Import Column API Mapping is created to map the column names with the API names. Custom settings has two fields Column Field Name(text) and Column API Name(text).

File Format object
Upload File Column object

Picklist values for the field Column Field Name are the field names of the object which records are being inserted. Example if Account records with the fields Account Name, Billing Address, Phone, Contact and Email are inserted then below will be the picklist values.


Note that picklist values contains the object field names but not the column names of the CSV files. Next is creating custom setting.

Import Column API Mapping custom setting

Now create the class ImportRecordsUtility.cls, the code is posted in GitHub.

Testing the tool

Below is the snapshot of the CSV file that needs to be inserted. The columns names are Name instead of Account Name, Address in place of Billing Address, Mobile which is Phone and Email for Contact Email.


Next create a new File Format record, name being anything, advisable to use the object name followed by the word file format like “Account File Format”. Now create the Upload File Column records. Column Name is same as the name of the columns in the CSV.


Now go to custom setting Import Column API Mapping to map the fields with the API names.


Upload the CSV file as Notes and Attachments to Accounts. Any other object would work as well, just have to query them correctly. Other than Notes and Attachments, it can be used in a visualforce page.

To call the class,

list<Account> newAccountRecords = ImportRecordsUtility.ImportRecords('Account','Account File Format'); // call the utility class and pass the parameters as the object name to insert and the file format you are using.

The inserted Accounts are as follows.screen-shot-2017-01-13-at-7-01-13-pm


View code on GitHub.

Apex CSV parser

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s