You need to sign in to do that
Don't have an account?

Is processing a csv file possible in Apex?
Hi Pals,
Is it possible to parse/process file uploaded( for e.g. a .csv file) in apex and and fetch records? From what I know, when a file is uploaded from suppose a VF page, we will receive a Blob of that file. But there is no mechanism as such to parse that Blob and see what data is exactly is inside it. I know this I/O processing can be easily done in .Net/Java. But usually a .Net/Java solutions is not what a customer expects because of the extra hosting/costing/whatever the other reasons. So, I want to know if this possible in Apex at all ? I searched boards and also IdeaExchange but did not find any confident answer. Any help is very much appreciated.
Thanks
Vishnu
Here is a method we use to help parse .csv files.
All Answers
Here is a method we use to help parse .csv files.
Here's another CSV parser that I use - http://blog.nicocrm.com/2011/03/06/parse-csv-file-in-salesforce-apex/ as it also handles embedded line breaks within a CSV field. Works great and can be used when receiving CSV files from non-US locations that use delimiters other than comma (like semi-colons, tabs, pipes, ...)
Thanks @Damien_. I thought same but did not implement that. I think I should try it. Thanks again!
Thanks for the response @cropzim. The url you shared does not show any article though.
The approach suggested by @Damien worked. But there is still an issue. It parses some csv files well but for some files it gives an error saying BLOB is not a valid UTF-8 string. I am looking for a way to resolve it. probably to look for UTF-8 decoding of blob.
nicocrm's blog can be found on Wodpress at https://nicocrm.wordpress.com/2011/03/06/parse-csv-file-in-salesforce-apex/
@cropzim Thanks for the annotated version.
Testing has now, of course, moved to a separeate class.