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
ssuede7ssuede7 

Attachment creation problems

We can not get an attachment object to submit to salesforce.  We take a file from a multipart form, we encode it in base64 and then use the setBody() method to enbed it in an Attachment object.  All of that seems to go fine.  Then we call the .create call and everything seems to work, but the attachment will not go though.  The only way to verify that it didn't work is to use the isSuccess() method on the saveResults object, which tells that it didn't.  We don't get an kind of error message.  Can anyone shed some light on this?  We have verified that the base64 converter is converting the data.  We have used the create call successfully many times to create cases and contacts, etc.  Any help would be greatly appreciated. Thanks in advance.

Here is some of the code, simplied of course:

//Create an attachment object

Attachment att = new Attachment();

//set a parent id from an existing case object
  ID id = new ID("50030000000c8adAAA");
  att.setParentId(id);


//set the body with a byte array that is outputed from the base64 encoder   
  att.setBody(Base64.encode(dataBytes));
  att.setName("testfile.txt");

//set the file size with an integer
Integer filesize = new Integer(formDataLength);
  att.setBodyLength(filesize);

//create an sobject array to hold the attachment object
  SObject[] atts = new SObject[1];
  atts[0] = att;

//submit the new attachment to salesforce using the create call
  SaveResult[] saveResults2 = sforceLogin.getBindingStub().create(atts);
  if(saveResults2[0].isSuccess()) {
    out.println("worked");
  } else {
    out.println("didn't work");
  }

DevAngelDevAngel

Hi ssuede7,

Do you get any information from the errors array on your result object?  If so, what does it say?

ssuede7ssuede7

First of all, we didn't realize we could get an error message back from the call, so thanks for that, second, here is the error we are getting:

Unable to create/update fields: BodyLength. Please check the security settings of this field and verify that it is read/write for your profile.

Haven't had much time to look into this, but the login we are using shouldn't have any restrictions so I don't think it's that.

Any thoughts?

DevAngelDevAngel
No, that's it.  The body length is determined by salesforce.com when the attachment is uploaded.
SuperfellSuperfell
You shouldn't be manually base64 encoding the attachment, Axis will automatically base64 encode it for you.