How to read csv file in apex salesforce

image

If you have a csv file with raw data and you want to read it using apex, convert to sfdc objects and insert these, then get success and failure results back then following steps and code can help you: 1. Create an Apex Class which will read the CSV file:

Full
Answer

How to import account data from CSV file in Salesforce?

CSV file attached has following format: Now we need to write code in Apex and visualforce which will read csv file and insert records in account object. Click on choose file, then select csv file and then click on ‘Import Account’ button. All records from csv file will be inserted on account records.

How to display imported records in Salesforce list?

If you are going to display imported records (which is a nice feature), you need to make sure the list you are displaying has less than 1000 elements. To insert the records into a Salesforce object, do something like this:

How to load CSVs from a page?

You could just use Data Loader to load CSVs, or even build a solution using the bulk API yourself. If you do need to do it from a page, I’d suggest that rather than splitting on a new line immediately, you use substring and the indexOf to pull just one line at a time from the input string, processing them as you go. Show activity on this post.

image


How do I read a csv file in Apex code?

Read and Insert records from a CSV file – Using VisualforceDownload the template from here. … Create an Apex Class named “FileUploader”. … Create a Visualforce Page named “UploadAccounts”. … Download the CSV file from here for test method coverage. … Create an Apex Class as shown below for test coverage.


How do I read a csv file in Salesforce?

Click on choose file, then select csv file and then click on ‘Import Account’ button. All records from csv file will be inserted on account records. I have commented insert account line. So this example will only show account records on visualforce page.


How do I read an Excel file in Apex?

First Salesforce does not read the excel file into apex. To overcome for this either we can covert excel file to CSV and import csv file using below code. And We can import data using data loader But sometime there is requirement when end users do not want to use Apex Data loader.


How do I read a CSV file from a static resource in salesforce?

Load data stored in Static ResourcesStep 1: Create CSV files with your data and upload them to Static Resources. … Step 2: Create a CSV Reader class. … Step 3: Adapt and run the following Anonymous Script on your Sandbox.


How do you read an apex attachment body?

You have to use att. body. toString() to get the content of the attachment. This only works if the blob is actually contains only text and not binary data.


How do you import a CSV file into lightning component?

How to import records using Lighting Web Component?STEP 1: Upload data on an account object. Sample CSV File:STEP 2: Select the CSV File of account records and then Upload.STEP 3: Create ReadCSVFileInLWC.html.