function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sslatersslater 

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!

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell
I think you're stuck until apex has an api for read/writing streams.

All Answers

SuperfellSuperfell
I think you're stuck until apex has an api for read/writing streams.
This was selected as the best answer
sslatersslater
How about at least adding a "fake" stream in a update. Just modify the getbody() method to take starting and ending offsets. Then we can at least emulate a stream by reading blocks. That should be a really easy change to the apex document method. Any odds of that?
SuperfellSuperfell
The problem with that is that for most character encodings (including utf-8) bytes != characters, so if you read based purely on byte ranges you can end up slicing a byte sequence that is a single character.
sslatersslater
bummer...thanks simon
JeremyKraybillJeremyKraybill

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

Cool_DevloperCool_Devloper

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 :smileysad: Would be really helpful if i can get something to refer to.

 

Thanks a Ton!!

Cool_D

sslatersslater

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:

 

String[] rowArray = inputArea.split('\\n');

for (String s : rowArray) {

  String[] colArray = s.split(',');

  for (String a : colArray) {

    //your work here

  }

}

sslatersslater

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.

Cool_DevloperCool_Devloper

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

anukarthi_nimmalaanukarthi_nimmala

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.