Salesforce Asked by Laureant on November 23, 2021
I have been tasked to create a batch apex loader which parses a CSV file from an email attachment.
I will use an email handler to get the email with the CSV file attached, and then I will try to load the attachment, and go through it, and create a new object for each line in it.
The CSV file will be sent from an outside source, and it can contain a lot of long text in some of the columns. Even line-breaks, and quotes can appear in some of the columns.
Is there a good, readily available CSV parser class/library in APEX, that I could use? One that handles all the cases, where there could be separators, quotes, end of lines INSIDE a value? Also, I would like to be able to handle all kinds of international & special characters.
I don’t want to re-invent the wheel, and spend a lot of time on writing & testing the parser, if there’s something already out there.
Thanks
Given that CSV's have a reasonably simple format defined in RFC 4180 it can be surprising complicated to actually implement a parser that is both correct to the spec and efficient for larger CSV files.
Support for possibly embedded line breaks, double quotes, and commas makes it more complicated. There isn't an easy way to divide the workload up in Apex because you can't split the incoming CSV into multiple lines safely. The only way to tell if a line-break or comma is actual data or part of the CSV format is to parse right up to it.
If you want to parse a larger CSV file that leaves you juggling CPU and Heap limits. Maybe Salesforce Functions will help in this scenario, but as at today I don't believe there is a robust way to do this for CSV files that start to get into the thousands of lines of records. E.g. 4 MB plus of data.
Of course, with Apex being multi-tenant there is always going to be an upper limit on the amount of data you can work with in one transaction. But as I mentioned above, there isn't an easy way to break a CSV down into manageable chunks unless you impose limits on how the CSV data is formatted.
Answered by Daniel Ballinger on November 23, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP