upload a file to salesforce using connector

断了今生、忘了曾经 提交于 2019-12-08 11:22:20

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/15919715/upload-a-file-to-salesforce-using-connector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!