In Salesforce classic it is easy to get the attachment id which is associate any object.
list<attachment> attlist= [select id from attachment where parentId='xxxxxxxx'];
But in the salesforce lightning environment, it is a little bit different
In lightning when you upload attachments, it saves as salesforce files. for querying salesforce files you need to understand three objects:
1.ContentVersion
2.ContentDocumentLink
3.ContentDocument
for each file uploaded in lightning, there will be only one contentDocument will get created. Contentversion depends upon you, default it will get created as version 1, later you can create n number of contentVersion. If you are uploading the document on a record, there will be two contentDocumentLink will get created. One will be related to you and others will be related to that particular record. later if you share the file with any other record or user, for each one, a contentDocumentlink will get created.
You can query all the latest contentversion as:
[select id,FileType, Title,FileExtension, Versiondata from ContentVersion where islatest=true ];
==================================================================
for(ContentDocumentLink c:[select ContentDocumentId from ContentDocumentLink where LinkedEntityId in :accId])
{
cntLinkId.add(c.ContentDocumentId);
}
attachList = [select Id, Title from ContentDocument where Id in :cntLinkId];
Get all in one list
contentdocumentLinkList=[select ContentDocumentId, ContentDocument.Title, ContentDocument.latestpublishedversionId, LinkedEntity.name from ContentDocumentLink where LinkedEntityId in: accId];
==============================================================
Controller:
public class downloadAllAttachment_ctrl {
public list<account>acclist {set;get;}///
public list<contentDocument>attachList {set;get;}
public list<id>attlistId {set;get;}
public list<account>accWithAtt {set;get;}
public list <contentdocumentLink>contentdocumentLinkList {set;get;}///
public downloadAllAttachment_ctrl()
{
accWithAtt=[select id,name, (select id,name from attachments) from account where name like 'Termi%'];
acclist=[select id, name from account ];
list<id>accId= new list<id>();
for(account a : acclist)
{
accId.add(a.id);
}
list<id> cntLinkId= new list<id>();
contentdocumentLinkList=[select ContentDocumentId, ContentDocument.Title, ContentDocument.latestpublishedversionId, LinkedEntity.name from ContentDocumentLink where LinkedEntityId in: accId];
}
}
===========================================
VisualForce Page:
<apex:page controller="downloadAllAttachment_ctrl">
<apex:pageBlock >
<!-- classic attachment --->
<apex:pageBlockTable value="{!accWithAtt}" var="a">
<apex:column value="{!a.name}" />
<apex:column >
<apex:pageBlockTable value="{!a.attachments}" var="b" >
<apex:column >
<apex:outputLink value="{!URLFOR($Action.Attachment.Download, b.id)}" target="_blank">{!b.name}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:column>
</apex:pageBlockTable>
<!-- Lightning Attachment-->
<apex:pageBlockTable value="{!contentdocumentLinkList}" var="a" >
<apex:column value="{!a.LinkedEntity.name}" />
<apex:column >
<apex:pageBlockTable value="{!a.ContentDocument}" var="b">
<apex:column >
<apex:outputLink value="/sfc/servlet.shepherd/version/download/{!b.latestpublishedversionId}?asPdf=false&operationContext=CHATTER" target="_blank">{!a.ContentDocument.Title}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:page>
Ref: ak@infogle.com
No comments:
Post a Comment