You need to sign in to do that
Don't have an account?
Workaround for String limits
Hi,
Does anyone have an idea of how to work around the string limits in this case.
I have an apex code that reads a document object. I can get that fine, and it's a CSV fine. I then want to run through the document and parse it line by line. I can get the body into a blob with document.body, then get the body as a string with the body.toString() method.
But the problem here is the limits on a string are 100,000 characters. If my blob is larger than 100,00 characters, then what can I do? I don't have to have it all at once. A loop is find but I don't see a way to do blob.tostring(start byte, end byte).
Any ideas?
Thanks!
All Answers
Well, if you're desperate and not averse to writing external hosted code, you *could* work around this situation by putting a little fetcher proxy in between that exposes some sort of rest API like
http://my.server.com/getDoc?docId=000000000000&page=1
or
http://my.server.com/getDoc?docId=000000000000&startOffset=0&endOffset=9999
Which gets your CSV from wherever (could be SF, could be somewhere else) and returns it in chunks of N characters at a time.
Then read that from Apex using HttpRequest.
For a good Java/.Net/PHP programmer, writing such a proxy should be less than a full day affair.
Yes, hackish, but if you are really desperate, you're not 100% out of luck!
Jeremy Kraybill
Austin, TX
Hello Sslater,
I am currently working on a similar scenario. Can you please let me know how can i parse my CSV file using APEX, which you have mentioned in your post?
I am tryin that out but somehow its not working for me Would be really helpful if i can get something to refer to.
Thanks a Ton!!
Cool_D
I just did an ugly hack and did not really parse CSV. SFDC really should have a CSV parser built-in. So I just did a split, which is not really CSV since CSV allows for commas if they are in double quotes, etc....
I had an input text area to start with:
for (String s : rowArray) {
String[] colArray = s.split(',');
for (String a : colArray) {
//your work here
}
}
If you read from a file, you have to upload into a Document, use Apex SOQL to fetch the Document and Document body, then use Document.getBody or something like that to get the body as a long string.
Thanks a lot Sslater!!
This was very helpful indeed to give me a good start up. Appreciate your kind help.
Furthermore, going through your post and other discussion posts as well, i understand that there is a constraint on the size of the String which will store the document/attachment content.
Did you try converting it into an XML document and then parsing this XML using APEX parser? Just wanted to know does the same limit applies if we go with this approach?
Many Thanks,
Cool_D
hi,
your idea is really good in parsing the csv file from the document folder.but iam not able to parse the columns in csv file so that i can insert this column values in to a custom object.So please help me in doing it.Any sample code would be helpful.