ABSYZ ABSYZ

  • Home

    Welcome to ABSYZ

  • About us

    Who We Are

  • Our Expertise

    What We Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

CSV parsing to sObjects

Home / Article & Blogs / Apex / CSV parsing to sObjects

CSV parsing to sObjects

By Souravmoy inApex, Salesforce

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).

screen-shot-2017-01-13-at-12-23-38-pm
File Format object

screen-shot-2017-01-13-at-12-27-02-pm
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.

screen-shot-2017-01-13-at-12-29-43-pm

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

screen-shot-2017-01-13-at-12-40-16-pm
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.

screen-shot-2017-01-13-at-8-05-58-pm

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.

screen-shot-2017-01-13-at-12-33-07-pm

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

screen-shot-2017-01-13-at-6-05-35-pm

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,

[sourcecode language=”java”]
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.
Database.insert(newAccountRecords);
[/sourcecode]

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

References:

View code on GitHub.

Apex CSV parser

36
Like this post
3 Posts
Souravmoy

Search Posts

Archives

Categories

Recent posts

Significance of UI/UX Design

Significance of UI/UX Design

Cyber-security in an uncertain world

Cyber-security in an uncertain world

The world of AR and VR

The world of AR and VR

The in-and-out of ML

The in-and-out of ML

Future of Mobility

Future of Mobility

  • Previous PostWhat is Enhanced Email ?
  • Next PostCreating a custom search Engine in Salesforce

Related Posts

Cyber-security in an uncertain world
Apex

Cyber-security in an uncertain world

REST API call from Einstein Analytics Dashboard
Apex REST Salesforce Salesforce Einstein Wave Analytics

REST API call from Einstein Analytics Dashboard

Create/Update Salesforce Picklist definitions using metadata API
Integration Metadata API Salesforce

Create/Update Salesforce Picklist definitions using metadata API

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

ABSYZ Logo
  • Home
  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

ABSYZ Software Consulting Pvt. Ltd.
USA: 49197 Wixom Tech Dr, Wixom, MI 48393, USA
M: +1.415.364.8055

India: 6th Floor, SS Techpark, PSR Prime, DLF Cyber City, Gachibowli, Hyderabad, Telangana – 500032
M: +91 79979 66174

Copyright ©2020 Absyz Inc. All Rights Reserved.

youngsoft
Copy