Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

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.

1 Answer

0 votes
by (32.1k points)
edited by

The following code allows you to upload a physical file to and attach it to a record.


  try {

        File f = new File("c:\java\test.docx");

        InputStream is = new FileInputStream(f);

        byte[] inbuff = new byte[(int)f.length()];        ;

        Attachment attach = new Attachment();




        // attach to an object in SFDC 


        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.

Go for this in-depth job-oriented Salesforce Online Training now!

Browse Categories