function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jaishrijaishri 

I want to display account,contact and opportunity Related files using lwc on account page please anyone help me to solve my problem

Best Answer chosen by jaishri
CharuDuttCharuDutt
Hii Jayati
Try Below Code
<template>
    <lightning-card title="File Preview v3" icon-name="custom:custom11">
      <div class="slds‐m‐around_small">
       <template if:true={filesList}>
          <table class="slds-table slds-table_bordered slds-table_cell-buffer">
             <thead>
                <tr class="slds-text-title_caps">
                   <th scope="col">
                      <div title="Key">File Name</div>
                   </th>
                   <th scope="col">
                      <div title="Value">File Extension</div>
                   </th>
                </tr>
             </thead>
             <tbody>
                <template for:each={filesList} for:item="keyValue">
                   <tr key={keyValue.Id}>
                      <th scope="col">
                         <div>{keyValue.Title}</div>
                      </th>
                      <th scope="col">
                         <div>{keyValue.FileExtension}</div>
                      </th>
                   </tr>
                </template>
             </tbody>
          </table>
       </template>
       </div>
    </lightning-card>
 </template>




import { LightningElement,api,wire } from 'lwc';
import cvRecords2 from '@salesforce/apex/contenVersionFilePreview.cvRecords2'
export default class ContentVersionV3 extends LightningElement {
    @api recordId;
    wiredActivities;
    filesList =[];

    @wire(cvRecords2, {recId: '$recordId'})
    wiredclass(value){
        this.wiredActivities = value;
        const { data, error } = value;
        if (data) { 
            console.log(data)
            this.filesList = data;
            console.log('Data========> '+JSON.stringify(this.filesList));
        }
        if(error){ 
            console.log(error);
        } 
    }
}



Apex

public class contenVersionFilePreview {
    @AuraEnabled(cacheable = true)
    public Static  List<ContentVersion> cvRecords2(String recId){
        set<Id> SetIds = new Set<id>();
        list<Account> lstAccount = [Select Id,(Select Id From Contacts),(Select Id From Opportunities) from Account where Id = :recId];
        for(Account Acc : lstAccount){
            SetIds.add(Acc.Id);
            for(Contact con : Acc.Contacts){
            SetIds.add(con.Id);
        	}
            for(Opportunity opp : Acc.Opportunities){
            SetIds.add(opp.Id);
        	}
        }
           List<ContentDocumentLink> files = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :SetIds];
            List<ID> fileIDs = new List<ID>();
            for (ContentDocumentLink docLink : files) {
                fileIDs.add(docLink.ContentDocumentId);
            }
            List<ContentVersion> docs = [SELECT Id,ContentDocumentId, FileExtension, Title 
                						 FROM ContentVersion WHERE ContentDocumentId IN :fileIDs];
          
            return docs;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

PriyaPriya (Salesforce Developers) 
Hi Jayati,

Please go through below link it has implementation similar to your use case.

https://www.w3web.net/display-account-related-contacts-in-lwc/

https://pritamshekhawat.wordpress.com/category/lightning-component/lightning-web-component/

If it helps please mark it as best answer.

Thanks!
CharuDuttCharuDutt
Hii Jayati
Try Below Code
<template>
    <lightning-card title="File Preview v3" icon-name="custom:custom11">
      <div class="slds‐m‐around_small">
       <template if:true={filesList}>
          <table class="slds-table slds-table_bordered slds-table_cell-buffer">
             <thead>
                <tr class="slds-text-title_caps">
                   <th scope="col">
                      <div title="Key">File Name</div>
                   </th>
                   <th scope="col">
                      <div title="Value">File Extension</div>
                   </th>
                </tr>
             </thead>
             <tbody>
                <template for:each={filesList} for:item="keyValue">
                   <tr key={keyValue.Id}>
                      <th scope="col">
                         <div>{keyValue.Title}</div>
                      </th>
                      <th scope="col">
                         <div>{keyValue.FileExtension}</div>
                      </th>
                   </tr>
                </template>
             </tbody>
          </table>
       </template>
       </div>
    </lightning-card>
 </template>




import { LightningElement,api,wire } from 'lwc';
import cvRecords2 from '@salesforce/apex/contenVersionFilePreview.cvRecords2'
export default class ContentVersionV3 extends LightningElement {
    @api recordId;
    wiredActivities;
    filesList =[];

    @wire(cvRecords2, {recId: '$recordId'})
    wiredclass(value){
        this.wiredActivities = value;
        const { data, error } = value;
        if (data) { 
            console.log(data)
            this.filesList = data;
            console.log('Data========> '+JSON.stringify(this.filesList));
        }
        if(error){ 
            console.log(error);
        } 
    }
}



Apex

public class contenVersionFilePreview {
    @AuraEnabled(cacheable = true)
    public Static  List<ContentVersion> cvRecords2(String recId){
        set<Id> SetIds = new Set<id>();
        list<Account> lstAccount = [Select Id,(Select Id From Contacts),(Select Id From Opportunities) from Account where Id = :recId];
        for(Account Acc : lstAccount){
            SetIds.add(Acc.Id);
            for(Contact con : Acc.Contacts){
            SetIds.add(con.Id);
        	}
            for(Opportunity opp : Acc.Opportunities){
            SetIds.add(opp.Id);
        	}
        }
           List<ContentDocumentLink> files = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :SetIds];
            List<ID> fileIDs = new List<ID>();
            for (ContentDocumentLink docLink : files) {
                fileIDs.add(docLink.ContentDocumentId);
            }
            List<ContentVersion> docs = [SELECT Id,ContentDocumentId, FileExtension, Title 
                						 FROM ContentVersion WHERE ContentDocumentId IN :fileIDs];
          
            return docs;
    }
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer
jaishrijaishri
Thank You Charu Soni But the File is not getting opened