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:
While trying above code you may get below error:
REST API TUTORIAL FOR 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.
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
Hi There,
ReplyDeleteSeems 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.
Thanks for posting this article. It helped definitely.
ReplyDeleteBut 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.
If file is not saved properly, then try changing this.
ReplyDeleteString fileContentType='pdf';
Change this to
String fileContentType='application/pdf';
it is still sowing fail to load pdf
ReplyDeleteI 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:
ReplyDeletepublic static Blob getURLBlobAsPDF(String url){
PageReference pageRef = new PageReference(url);
Blob ret = pageRef.getContentAsPDF();
return ret;
}
Hi, this is really great.
ReplyDeleteI 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
Hi Dude did you get a solution for this. I have exactly the same requirement
DeleteHi, this is helpful.
ReplyDeleteI 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
what should be the contenttype for doc files?
ReplyDelete