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
bhanu_prakashbhanu_prakash 

create LWC component to display files and attachment with page navigation

Hi Team,
I am trying to display files and attachment of account with next and pervious button. Need to display single file on layout after click need to open file in popup
User-added image
apex 
public with sharing class ct_FileController { 

    @AuraEnabled
     public static List<ContentDocument> getFiles(Id recordId) {
       List<ContentDocument> documents = [SELECT Id,Title  FROM ContentDocument        
            WHERE Id IN (SELECT ContentDocumentId 
            FROM ContentDocumentLink 
            WHERE LinkedEntityId = :recordId)];        

        return documents;
    }
}


LWC Html 
<template>
    <lightning-card title="File Viewer" icon-name="custom:custom14">
        <div class="slds-m-around_medium">
          <p>hello </p>
            <p>{fileName}</p>
            <lightning-button label="Previous" class="slds-m-right_x-small" onclick={previous}></lightning-button>
            <lightning-button label="Next" class="slds-m-left_x-small" onclick={next}></lightning-button>
        </div>
    </lightning-card>
</template>

LWC JS 
import { LightningElement, api, wire } from 'lwc';
import  getFiles from '@salesforce/apex/FileController.getFiles';
import LightningPrompt from 'lightning/prompt';

export default class iframe extends LightningElement {
    @api recordId;
    fileIndex;
    fileName;
    @wire(getFiles, { recordId: '$recordId'})
    files;
    connectedCallback(){
        this.fileIndex = 0;    
        if(this.files.data){
            this.fileName = this.files.data[this.fileIndex].Name;
                        console.log("Line no 14 :::fileName" , fileName);
                console.log("Line no 15 :::files" , files.data);
        }
    }

    next(){
        LightningPrompt.open({
            message: 'Next button click',
            //theme defaults to "default"
            label: 'Please Respond', // this is the header text
            defaultValue: 'initial input value', //this is optional
        })
        if (this.fileIndex < this.files.data.length - 1) {
            this.fileIndex++;
            this.fileName = this.files.data[this.fileIndex].Name;
        }
    }

    previous(){
        LightningPrompt.open({
            message: 'Previous button click',
            //theme defaults to "default"
            label: 'Please Respond', // this is the header text
            defaultValue: 'initial input value', //this is optional
        })
        if (this.fileIndex > 0) {
            this.fileIndex--;
            this.fileName = this.files.data[this.fileIndex].Name;
        }
    }
}

But Iam unable to see file in layout need help to debug

 
Shri RajShri Raj

In my understanding, The code you posted has a few issues.
The Apex class is missing a return type for the getFiles method. You need to specify the return type, in this case, it is a list of ContentDocument.
In the connectedCallback method, you are trying to access the 'Name' property of the ContentDocument object, but the field name is 'Title'.
The LightningPrompt component is not being used properly, it's not really related to the problem you are facing and you can remove it.

 

public with sharing class ct_FileController { 

    @AuraEnabled
    public static List<ContentDocument> getFiles(Id recordId) {
        List<ContentDocument> documents = [SELECT Id, Title  FROM ContentDocument        
            WHERE Id IN (SELECT ContentDocumentId 
            FROM ContentDocumentLink 
            WHERE LinkedEntityId = :recordId)];        

        return documents;
    }
}
 
import { LightningElement, api, wire } from 'lwc';
import  getFiles from '@salesforce/apex/FileController.getFiles';

export default class Iframe extends LightningElement {
    @api recordId;
    fileIndex;
    fileName;

    @wire(getFiles, { recordId: '$recordId'})
    files;

    connectedCallback(){
        this.fileIndex = 0;    
        if(this.files.data){
            this.fileName = this.files.data[this.fileIndex].Title;
        }
    }

    next(){
        if (this.fileIndex < this.files.data.length - 1) {
            this.fileIndex++;
            this.fileName = this.files.data[this.fileIndex].Title;
        }
    }

    previous(){
        if (this.fileIndex > 0) {
            this.fileIndex--;
            this.fileName = this.files.data[this.fileIndex].Title;
        }
    }
}