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
Patrick BulaczPatrick Bulacz 

String Character Limits (creating large blob attachment).

Hi All,

 

Just wondering if anyone has had any experience with working around the 100000 char limit on Strings in Apex.

 

Basically am producing a large text file that needs to then be attached to a record via notes and attachments.

 

I've got a few workarounds but wondering if there are any other ideas out there.

 

Ideally I'd like to check the length of the string and if it's 'nearing' the limit convert it to a blob and add it to a list of blobs and then run a loop and add all the blobs together to create 1 large file and then attach that via notes and attachments. Obviously that can't be done because you can't just simply add blobs but that's the basic dilemma so if anyone has any suggestions please let me know.

 

My other solutions are to generate the output to a visualforce page then scrape the visualforce page but I get the ugly <Html> and </Html> tags at the beginning and the formatting is a little off (would be good to just be able to 'renderAs=text' but oh well.

 

Anyway any help is much appreciated.

 

Cheers,

 

Patrick

Best Answer chosen by Admin (Salesforce Developers) 
wesnoltewesnolte

Hey

 

What about this:

 

 

Blob myBIGblob;
for(Blob thisB:myBlobs){
// unfortunately can't do the following with binary types like you can
// with strings :(
myBIGblob.valueOf(myBIGblob.toString() + thisB.toString());
}

All Answers

wesnoltewesnolte

Hey

 

Why not make a list of strings and then put those all into a blob at the end?

 

Cheers,

Wes 

Patrick BulaczPatrick Bulacz

Nope that won't work. Unless you know something I don't??? (I hope you do .. fingers crossed).

 

Say I do this... look at the code below...

 

 

// Say here I only know one way to cast a string to a blob and that is // using Blob.ValueOf(myString); // So if I want my list of strings into 1 BIG Blob, I have to add up all // my seperate strings and then convert to a blob using the cast // However I'll hit the 100000 Char limit there. String myBIGstring; for(String thisS:myStringList){ // obviously going to hit the limit here. myBIGstring += thisS; } Blob.valueOf(myBIGstring); // Okay so maybe I try something else... // Maybe I try converting all my strings into a list of blobs and // then add the blobs... PROBLEM: Can't add blobs :( List<Blob> myBlobs = new List<Blob>(); for(String thisS:myStringsList){ myBlobs.add(Blob.ValueOf(thisS)); } Blob myBIGblob; for(Blob thisB:myBlobs){ // unfortunately can't do the following with binary types like you can // with strings :( myBIGblob += thisB; }

 

 In a dilly of a pickle there wouldn't you say?

 

 

wesnoltewesnolte

Hey

 

What about this:

 

 

Blob myBIGblob;
for(Blob thisB:myBlobs){
// unfortunately can't do the following with binary types like you can
// with strings :(
myBIGblob.valueOf(myBIGblob.toString() + thisB.toString());
}
This was selected as the best answer
Patrick BulaczPatrick Bulacz

Hey Wesnolte,

 

Thanks for that... it worked!!!

 

I just needed to change 1 little thing but it worked!!!

 

See below for my code just a slight change of syntax.

 

Really didn't think this was going to work because  I thought casting the blob that already has 100000+ characters in it to a string then casting back would throw the exception. But nope!!

 

Well done mate.

 

And thanks again!

 

 

for(blah blah blah here){

if(myString.length() < 100000 && myString.length() > 95000){
myBlobs.add(Blob.valueOf(myString));

myString = ''

}

}

 

// basically what you had just the little change of syntax there.

 

// this initialises the blob but also just makes sure it's not null for when we cast it for the first time

myBIGblob = blob.valueOf('');

 

// loop through list of blobs

for(Blob thisB:myBlobs){ myBIGblob = blob.valueOf(myBIGblob.toString() + thisB.toString()); }