You need to sign in to do that
Don't have an account?
Rahul Kumar Dey
Return files in url format associate with record (Example- Case) using REST API webservice?
Hi All,
I have created a rest api webservice to insert contentVersion into salesforce but now I want to return the list of files to external system using rest api webservice. And I don't want to send exact files associated with case instead I want to send url so, external user can easily access files from this url (list of url).
Any help how to proceed?
And any sample code really appreciate...
Best Regards,
Rahul
I have created a rest api webservice to insert contentVersion into salesforce but now I want to return the list of files to external system using rest api webservice. And I don't want to send exact files associated with case instead I want to send url so, external user can easily access files from this url (list of url).
Any help how to proceed?
And any sample code really appreciate...
Best Regards,
Rahul
Can you please try creating a public link manually.
1. Get either of ContentDocument Id or ContentVersion Id of the file associated with your case.
2. Navigate to https://<your-instance>.my.salesforce.com/contentId where contentId is the one you got from step one.
3. Now create a public link for the file.
Now try calling the API you created with the same case Id.
If it works,
Create a new instance of ContentDistrubution and add ContentVersionId & ContentDocumentId to it & insert it.
I hope it helps you.
___________
Regards,
Sachin
(:
All Answers
If you're talking about the public link of the ContentVersion file. You can achieve this using ContentDistributionUrl field of ContentDistribution SObject.
To get the public link of the file, You need to query over ContentDistribution SObject using ContentDocumentId of the file you inserted.
Please follow this (https://sfdclesson.com/2019/03/17/show-the-uploaded-image-on-the-detail-page/" target="_blank)link to achieve the functionality.
Or if you want download link of the ContentVersion File, you simply need to get the host URL & append ContentVersion Id with it.
This is the link where your file will be stored.
In case you still have any doubts. Feel free to post them here.
______
Regards,
Sachin
(:
Thanks for reply--
Please check my code below-----
Right now I only return attachment name for testing purposes but we want to implement that, return url which is publicly accessable file and download.
Please note I created httpPost method in separate api. So, I'm not creating file again here.
Any luck based on my code? How to achieve...
Thanks,
Rahul
I've updated your code. Please update the code from line 65 to 103 at your end.
If any problems occur, please post them here.
________
Regards,
Sachin
(:
After I snipped your code, it returns null value both for case History and attachments.
But I have 2 files associated with this case...
USER_DEBUG [87]|DEBUG|List has no rows for assignment to SObject
Its because we've made a mistake during querying ContentDistribution.
Please update the query to & let me know if it worked. If you've any other issues, please post them here.
_____________
Regards,
Sachin
(:
Please update the code like this.
And my try block look like this-
where we do wrong ...
But in AttachmentList contains list of files
Can you please try creating a public link manually.
1. Get either of ContentDocument Id or ContentVersion Id of the file associated with your case.
2. Navigate to https://<your-instance>.my.salesforce.com/contentId where contentId is the one you got from step one.
3. Now create a public link for the file.
Now try calling the API you created with the same case Id.
If it works,
Create a new instance of ContentDistrubution and add ContentVersionId & ContentDocumentId to it & insert it.
I hope it helps you.
___________
Regards,
Sachin
(:
Your last solution really working fine, but unfortunately we don't go with it because of security reason. The thing is we never give external user any salesforce instance access to download files via url. So, we decide to go with 3rd party services to store files over there and we only store the url in salesforce which is generate by 3rd party.
But i'm going to give your last answer as best because, if there is no concern about security it means external user also have salesfore access. Thats properly fine...
Thanks,
Rahul
You don't need anyone to access your org. I told to create it manually because I was confused about whether ContentDistribution gets created itself like ContentDocument when ContentVersion is added.
So, the answer is NO.
One thing I noticed is, the ContentDistribution gets created when the user clicks on generate public URL(like I told you to do this manually) or you need to create one through your apex code. So you do have an option to create it using Apex. You can do it like. In case you've any other doubt, please post them.
_________
Regards,
Sachin
(: