You need to sign in to do that
Don't have an account?
manoj6492
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 ...
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
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.