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 Sample Code

Can anyone provide some working sample code of how to create and submit an attachement in JSP?  This would be much appreciated.

DevAngelDevAngel

Here you go. You'll notice that there is no mention of base64 as this is coming from a file stream. If you are creating an attachment from a text string, you will need to encode as base 64. There is a base64 lib in Axis, but I don't know how good it is.



private void DoAttachment() throws IOException {
File f = new File("test.doc");
InputStream is = new FileInputStream(f);
byte[] inbuff = new byte[(int)f.length()];

is.read(inbuff);
Attachment attach = new Attachment();
attach.setBody(inbuff);
//Do not attempt to set body length, this calculated
//be salesforce.com
//attach.setBodyLength(new Integer(inbuff.length));
attach.setName("text.doc");
ID parentId = new ID();
parentId.setValue("00330000006ZpSZ");
attach.setParentId(parentId);
SoapBindingStub b = loginEnterprise("mobile2@user.com", "pass");
SaveResult sr = b.create(new com.sforce.soap.enterprise.sobject.SObject[] {attach})[0];
if (sr.isSuccess())
System.out.println("Successfully added attachment.");
else
System.out.println("Error adding attachment: " + sr.getErrors(0).getMessage());
}

Message Edited by adamg on 02-17-2005 11:36 AM