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
manoj6492manoj6492 

Adding an attachment to the account using java

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 ...

sambasamba

As first, We must confirm attach.setBody is correct.

 

1.  Can you insert a contact to salesforce?

 

2.  You can try to add a string character to setBody. Like attach.setBody("Hello Test");

 

3.  If your code still does not work, please sent error message to my email.

 

Hope this helps.

 

Thanks,

Samba

guruprasad hubliguruprasad hubli
Hi . get an error at ,
SaveResult sr = binding.create(new com.sforce.soap.enterprise.sobject.SObject[] {attach})[0];

error
Multiple markers at this line
    - binding cannot be resolved
    - Null pointer access: The variable binding can only be null at this 
     location
    - The value of the local variable sr is not used

Can you please help.
Castulo DEVCastulo DEV
private static final String LOGIN_URL ="https://eu13.salesforce.com/services/Soap/u/38.0"
final ConnectorConfig loginConfig = new ConnectorConfig();
            loginConfig.setAuthEndpoint(LOGIN_URL);
            loginConfig.setServiceEndpoint(LOGIN_URL);
            loginConfig.setManualLogin(true);

            final PartnerConnection binding = Connector.newConnection(loginConfig);


..................

 SaveResult[] results = binding.create(new SObject[] { attach});
            if (results[0].isSuccess())
                System.out.println("attach: " + results[0].getId());
            else
                System.out.println("Error: " + results[0].getErrors()[0].getStatusCode() +
                        ":" + results[0].getErrors()[0].getMessage());