How to download a file from salesforce

0
Hello Experts,    I have a file document in Salesforce, and I have the URL for the file document and credentials for the same.    I just want to access/download  the file, when I hit URL in browser, it prompts me for a password. After entering the password, the file dcoumet is displayed in browser, and I have the option to download it.     Now I need to implement this logic in Mendix. When a user clicks the "Download File" button in Mendix, it should bypass the password and download the file in Mendix directly.    How can I achieve the same in Mendix, is there any way to do it by downloading some modules from market place to integrate the sales force? Second how I can bypass (Pass the password in the backend where the user should not enter it) the password?   DOCUMENT URL: "https://drlibtdemo2021--qa.sandbox.my.salesforce.com/sfc/p/8F0000008iQb/a/8F0000008fgO/cpDqjugUyoZ28qdiOI6K9cPjoDEgAoIoSiY.mggguDA"    Method:   I have tried in the below way to bypass the password through backend.         After this i can able to download the file but the file type is coming has "File" so even after downloaded i couldn't able to view the content.   Answers are really much appreciated!
asked
1 answers
0

if you use REST activity, it will return the content without file type extension. 

You may need to write java action to make http request, the result of http request maybe InputStream

 

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public InputStream makeHttpRequest(String fileUrl, String accessToken) throws Exception {
    URL url = new URL(fileUrl);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

    connection.setRequestMethod("GET");
    connection.setRequestProperty("Authorization", "Bearer " + accessToken);
    connection.setConnectTimeout(5000); // Timeout for connecting to the server
    connection.setReadTimeout(5000); // Timeout for reading the data from the server

    int responseCode = connection.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_OK) {
        throw new Exception("HTTP request failed with response code " + responseCode);
    }

    return connection.getInputStream();
}

InputStream fileStream = makeHttpRequest(salesforceFileUrl, accessToken);

 

Then you can store in mendix FileDocument, you can create the filedocument from microflow as an input in java action

 

private technoapi.proxies.GeneratedExcel GeneratedExcel; // if this was an input these line will auto generate by mendix

GeneratedExcel.setName("ExcelOutput.xlsx"); //Set the file type in code or you can set it in microflow when creating obj

Core.storeFileDocumentContent(context, GeneratedExcel.getMendixObject(), fileStream); // fileStream will get from the function makeHttpRequest

return GeneratedExcel.getMendixObject(); // in java action return it as FileDocument as GeneratedExcel is an object generalization from FileDocument.

 

answered