Below is my code for Lightning Web Component:
HTML File:
<template>
<template if:true = {files.data}>
<template for:each={files.data} for:item='file'>
<lightning-card key={recordId} icon-name={file.Title}></lightning-card>
</template>
</template>
<template if:true={error}>
<c-error-panel errors={error}></c-error-panel>
</template>
</template>
JS FIle:
import { LightningElement, wire, track, api } from 'lwc';
import getAllRelatedAttachments from '@salesforce/apex/filesWebComponentController.getAllRelatedAttachments';
export default class lightning extends LightningElement {
@track files={data:[]};
@track error;
@api recordId;
@wire (getAllRelatedAttachments,{recordId:"$recordId"})
getallfiles({data,error}){
if (data) {
this.files = data;
this.error = undefined;
} else if (error) {
this.error = error;
this.files = undefined;
}
}
}
My Apex Controller:
public with sharing class filesWebComponentController {
public filesWebComponentController() {
}
@AuraEnabled(Cacheable=True)
public static List<ContentVersion> getAllRelatedAttachments(Id recordId){
set<id> parentIDs=new set<id>();
List<Contact> conList = [select Id From Contact where id=:recordId];
for(Contact con:conList){
parentIds.add(con.Id);
}
if(parentIDs.size()>0){
system.debug('ParentIDS'+parentIDs);
}
List<ContentDocumentLink> files = [SELECT ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN :parentIDs];
List<ID> fileIds = new List<ID>();
for (ContentDocumentLink docLink : files){
fileIds.add(docLink.ContentDocumentId);
}
if(!fileIds.isEmpty()) {
List<ContentVersion> file = [SELECT Id, Title, ContentDocumentId FROM ContentVersion WHERE ContentDocumentId IN :fileIds];
return file;
}
else{
return Null;
}
}
}
{}
button in the editor. For inline code, use the backtick (" ` ") character.