• Castulo DEV
  • NEWBIE
  • 0 Points
  • Member since 2020
  • Java Developer
  • APM


  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 1
    Likes Given
  • 0
    Questions
  • 2
    Replies

hi

import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.DeleteResult;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.Error;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.SaveResult;
import com.sforce.soap.enterprise.sobject.Account;
import com.sforce.soap.enterprise.sobject.Contact;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class Attachment {
static final String USERNAME = "XXXX";
static final String PASSWORD = "XXXX";
  static EnterpriseConnection connection;

  public static void main(String[] args) {

    ConnectorConfig config = new ConnectorConfig();
    config.setUsername(USERNAME);
    config.setPassword(PASSWORD);
    //config.setTraceMessage(true);

    try {

      connection = Connector.newConnection(config);

      // display some current settings
      System.out.println("Auth EndPoint: "+config.getAuthEndpoint());
      System.out.println("Service EndPoint: "+config.getServiceEndpoint());
      System.out.println("Username: "+config.getUsername());
      System.out.println("SessionId: "+config.getSessionId());

      // run the different examples
        createAttachment();
    // createAccounts();
      //updateAccounts();
     // deleteAccounts();


    } catch (ConnectionException e1) {
        e1.printStackTrace();
    }  

  }
  public static void createAttachment()
  {try {

        File f = new File("c:\\java\\test.docx");
        InputStream is = new FileInputStream(f);
        byte[] inbuff = new byte[(int)f.length()];        
        is.read(inbuff);

        Attachment attach = new Attachment();
        attach.setBody(inbuff);
        attach.setName("test.docx");
        attach.setIsPrivate(false);
        // attach to an object in SFDC 
        attach.setParentId("a0f600000008Q4f");

        SaveResult sr = binding.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());
        }


    } catch (FileNotFoundException fnf) {
        System.out.println("File Not Found: " +fnf.getMessage());

    } catch (IOException io) {
        System.out.println("IO: " +io.getMessage());            
    }
  }

 I am tryin to add an attachment but my code asks me to create methods for setbody,setname...y should i create methods for that....anyone can help me ...

Hi --

I've looked through the discussion board and found some examples of how to create an attachment (see below) using Java.  However, they all refer to the ID object which I do not have in 9.0 API.  Is there something I could use instead of the ID object?  Is the code below the best way to create an  attachment? 

Any and all help is appreciated.

Thanks,
jd

=====================
Sample Code
=====================
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());
}
  • June 27, 2007
  • Like
  • 1
Hi --

I've looked through the discussion board and found some examples of how to create an attachment (see below) using Java.  However, they all refer to the ID object which I do not have in 9.0 API.  Is there something I could use instead of the ID object?  Is the code below the best way to create an  attachment? 

Any and all help is appreciated.

Thanks,
jd

=====================
Sample Code
=====================
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());
}
  • June 27, 2007
  • Like
  • 1