2

I have a piece of code that show above error on

     d.FolderId = UserInfo.getUserId();
     d.Name = 'Data2'; 
     String myContent = this.name+ '\n ' +this.pass+ '\n ' + this.age+ '\n ' +this.mail; 
     d.body += Blob.valueof(mycontent);
     d.ContentType = 'text/plain';
     d.Type = 'txt';
     upsert d;
     return null;
 }

 d.body += Blob.valueof(mycontent);(Show error in this line)
7

2 Answers 2

2

Blob just doesn't work that way. You need to build up one string, and call Blob.valueOf at the end. For instance, you could do:

Blob.valueOf(myContent + myContent);

Which seems to be what you want in your example. Or, as an alternative to the code in the flagged duplicate:

Blob.valueOf('s1' + 's2' + 's3');
2

If you want to append to the Body, you first need to extract the value of the Body Blog as a string so you can append. Try this:

Blob firstBlob = Blob.valueOf('test');
String firstString = firstBlob.toString();
Blob secondBlob = Blob.valueOf(firstString + 'test');
System.debug(secondBlob.toString());

For a concrete example, try this:

Account acct = [SELECT Id FROM Account LIMIT 1];
Attachment attch = new Attachment();
attch.ParentId = acct.Id;
attch.Name = 'Data2';
attch.Body = Blob.valueOf('test');
insert attch;

String blob2String = attch.Body.toString();
Blob secondBlob = Blob.valueOf(blob2String + 'secondtest');
System.debug(secondBlob.toString());

attch.Body = secondBlob;
update attch;
system.debug(LoggingLevel.ERROR,attch);

This outputs 'testsecondtest' for me.

I'm not sure of what the d object is, but I suspect that you should try to catch the error on insert - because it's probably unrelated to the Blob

6
  • But that is way less efficient if you already know the string value of firstBlob, no?
    – Adrian Larson
    Aug 30, 2016 at 18:13
  • Sure, but I think @Bonny to append to an already encoded Body from what they said. Aug 30, 2016 at 18:16
  • Right, but my point is that it sounds like firstString is already known. That's why I didn't include this approach, anyway. Still, if you didn't already know firstString, this would be the way to go, you're right.
    – Adrian Larson
    Aug 30, 2016 at 18:18
  • 1
    Good point - oh well, bonny now has both options :) Aug 30, 2016 at 18:19
  • Hi Casper I did Some changes in my code but its not Append still Here is my Code d.FolderId = UserInfo.getUserId(); d.Name = 'Data2'; system.debug('d.body==='+d.body); String myContent = this.name+ '\n ' +this.pass+ '\n ' + this.age+ '\n ' +this.mail; d.body= Blob.valueof(mycontent); string s1 = d.body.tostring()+mycontent;string s1 = d.body.tostring(); d.body = blob.valueof(s1); d.ContentType = 'text/plain'; d.Type = 'txt'; upsert d; return null;
    – Bonny
    Sep 5, 2016 at 7:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .