upload a file to SalesForce programmatically
Asked Answered
B

2

5

I build a flow which create a case in a salesforce. Now, I am trying to upload a file to that case programmatically. Basically the client uploads the file manually in the server. now the question is how could I upload it by java rest based web service. it's good to have any links which describe about this topic.

Benny answered 5/4, 2013 at 9:33 Comment(1)
Did you read this : Salesforce REST API salesforce.com/us/developer/docs/api_rest ?Silin
R
5

First try to read the documents below:
See the following:
http://www.salesforce.com/in/?ir=1
API doc:
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_document.htm
Examples:
http://login.salesforce.com/help/doc/en/fields_useful_field_validation_formulas.htm

The following code allows you to upload a physical file to Salesforce.com and attach it to a record.
Code:

  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());            
    }

Please try this and let me know in case of any concern.

Redevelop answered 5/4, 2013 at 9:37 Comment(4)
"Attachment attach = new Attachment();" Where does this 'Attachment' come from ? if it is in the api, then can u please suggest which api i need to include also.Benny
"Attachment attach = new Attachment();" ---- or does we need to create a class of Attachment ?Benny
This is a library class. have you added all jars? please read all docs after that start your work.this will help you.Redevelop
ya, i added the jars from which i can possibly create a case in salesforce without any problem. Can you please mention the name of the jar file from which i can get the 'Attachment' library class file ? so that i can make sure that i had included it or not.Benny
I
2

Previous answer gave API doc for Document instead of Attachment. API Doc for Attachment is: http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_attachment.htm

Infringement answered 17/12, 2013 at 14:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.