[SalesForce] How to get content of Attached file lightning

I need to display content of attachment within iframe that is embedded in the particular sObject.

When you upload file from notes&Attachments related list on salesforce classic its type is attachment
and i have hard coded link for displaying content of this attachment

/servlet/servlet.FileDownload?file='[id of attachment]'

But when i upload this file from lightning its type is file.

so is it possible to display content of ContentVersion I guess or what its type is in iframe in the same way as attachemnt in my example?
enter image description here
enter image description here

Best Answer

You will need to query the contentLinkDocument records with the EntityId equal to the record Id .

And also ContentLinkDocument will give you Id of the content Document and you will need a query against content document to get the necessary file

SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = '[RECORD ID]'

Then SOQL on ContentDocument

Select Id ,Title from ContentDocument Where ID In :[CONTENTDOCUMENTLIST]

Update

If you use Files or content related object there is a simple event with new lightning namespace which can be used

Here is a sample quick POC

<aura:component implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes" access="global" >
  <aura:attribute name="contentId" type="String" default="069B00000015QMwIAM"/>
 <lightning:button variant="brand" label="Preview Reciept" onclick="{!c.preview }" />

Note that the default here is the Id of the content document .

The controller code for same is below

({
  preview : function(component, event, helper) {
   $A.get('e.lightning:openFiles').fire({
    recordIds: [component.get("v.contentId")]
   });
  }
})
Related Topic