Question

i builded a flow in mule which create a case in Salesforce using 'Salesforce' connector. now i need to upload a file to that case using the same mule flow. This can be done programatically by the following 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());            
}

But to make it simple, does there is any mule connector which automatically done all this and attach a file to the particular created case.

Was it helpful?

Solution

Yes you can use the Salesforce Cloud Connector for that. Example:

<file:file-to-byte-array-transformer />
<sfdc:create type="Attachment">
    <sfdc:objects>
        <sfdc:object>
            <body>#[payload]</body>
            <name>test.docx</name>
            <parentid>#[message.inboundProperties['mysfdcparentid']]</parentid>
        </sfdc:object>
    </sfdc:objects>
</sfdc:create>

In the example, I am setting the sobject type to 'Attachment'.

The body element is the file itself. Note that the connector will handle the base64 encoding for you, you just need to provide it with a byte array. If you're using File, you can use file:file-to-byte-array-transformer for example.

The parentid is set using MEL to get the value from a message property. So if you have a previous SFDC operation you can use MEL to extract the value of the previous sobject.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top