Saturday, February 25, 2017

Fetching file from external/public URL and storing it into Salesforce

If you have file URL for file stored outside the salesforce, then you can fetch file information from external URL by performing a call out to external system and store the file in salesforce.

For Demo purpose, I have uploaded a pdf file in Google drive and shared it with link with people. Now I will fetch this file and will store it as attachment in salesforce under account record.

File URL-  https://drive.google.com/file/d/0ByXILxflqQ2jWGpNVmI1WW9uYTQ/view?usp=sharing


Below is apex class which will help us to perform this activity:



Now you can run below code in developer console to test this:

//you can specify any record Id where you want to store file as attachment
String RecordId='0019000000ld4kN'; 
String fileContentType='pdf';
String extFileURL='https://drive.google.com/file/d/0ByXILxflqQ2jWGpNVmI1WW9uYTQ/view?usp=sharing';
blob fileBlob=FileDownLoadUtility.fetchFileFromExternalUrl(extFileURL);
Id attachmentId = FileDownLoadUtility.createAttachment(fileBlob, RecordId , fileContentType);
system.debug('*****attachmentId:'+attachmentId);

Note: 

  • If you are trying to fetch file from external source which is authenticated, then pass authorization parameters in HTTP Request headers
  • You should specify the file content type before creating attachment in order to properly view file.
  • You can add additional parameter as fileName in "createAttachment" method, if you you want specify the attachment name while creating attachment.

While trying above code you may get below error:

For this you need to add external URL in Remote Site Settings. For this example, I have added "https://drive.google.com/" in remote site settings.




More Blogs>>: BOX AND SALESFORCE INTEGRATION    
INTEGRATING BOX FILE PICKER WITH SALESFORCE
REST API TUTORIAL FOR SALESFORCE




9 comments:

  1. Hi There,

    Seems like I won the lottery here….This is a treasure box of blogs and your folks are like leprechauns! Phenomenal read on Fetching file from external/public URL and storing it into Salesforce !

    when user select an account from the picklist, all opportunities which are related to that account should be displayed in a table with checkbox in a first column of each row, there should be one master checkbox in the header of the table to select or deselect all opportunities in one click.

    By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).

    Kind Regards,
    Preethi.

    ReplyDelete
  2. Thanks for posting this article. It helped definitely.
    But i noticed that the original format of the file is not preserved. With this code, we only get binary content, which is not helpful.
    If you use ContentVersion and ContentDocumentLink, then we can extract the file as it is in salesforce.

    ReplyDelete
  3. If file is not saved properly, then try changing this.

    String fileContentType='pdf';
    Change this to
    String fileContentType='application/pdf';

    ReplyDelete
  4. it is still sowing fail to load pdf

    ReplyDelete
  5. I think the issue is related to some additional headers that google drive adds to the content PDF result you can see them if you evaluate the Blob, you should also take a look at the following code:

    public static Blob getURLBlobAsPDF(String url){
    PageReference pageRef = new PageReference(url);
    Blob ret = pageRef.getContentAsPDF();
    return ret;
    }

    ReplyDelete
  6. Hi, this is really great.
    I am new to Salesforce Dev.
    Does someone could help me to get this into a Class to be called from a Trigger to do the following?
    Basically, I will have the external file URL stored into a text field (custom object - e.g. Custom_object__c) and before insert/update of that field (e.g. file_url__c) I would like to insert an attachment on that specific record using that file.

    Another field (Content_type__c) on the same object I will have the ContentType (e.g. application/pdf) as the external fields would not all be pdf I need to get this dynamic.

    Many thanks for any kind of help
    Vale

    ReplyDelete
    Replies
    1. Hi Dude did you get a solution for this. I have exactly the same requirement

      Delete
  7. Hi, this is helpful.
    I want to fetch the google drive folders on button click using VF page and displays all the files. After selection of one file it will insert into the opportunity attachment record.

    Thanks in advanced.
    Bhavika

    ReplyDelete
  8. what should be the contenttype for doc files?

    ReplyDelete