I'm having issues with opening a Content Document preview in my LWC component. Here is my code:
HTML:
<template>
<template if:true={files}>
<ul>
<template for:each={files} for:item="i">
<template if:true={i.Title}>
<li key={i.Id}><a data-id={i.Id} onclick={navigateToFiles}>{i.Title}</a></li>
</template>
</template>
</ul>
</template>
</template>
JavaScript:
import { LightningElement, api, track, wire } from 'lwc';
import getAttachmentFiles from "@salesforce/apex/Utility.getAttachmentFiles";
import { NavigationMixin } from 'lightning/navigation';
export default class ShowAttachmentDetails extends LightningElement {
@api recordId;
@api files = [];
renderedCallback(){
}
@wire(getAttachmentFiles, {recordId: '$recordId'})
wiredAttachments({ error, data }){
if (this.recordId){
if (data){
this.files = data;
} else if (error){
//error handling
}
}
}
navigateToFiles(e){
const attachmentId = e.target.dataset.id;
if(attachmentId){
console.log('Selected Id -- ' + attachmentId);
this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
pageName: 'filePreview'
},
state: {
recordIds: attachmentId,
selectedRecordId: attachmentId
}
});
}
}
}
When I click on the link, I see the console line and then nothing else happens. I got the above code from Salesforce documentation available here: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/use_open_files
For those wondering why I need to write a custom component instead of using a related list, this is for one of those standard objects where there are no out of the box related lists. Unfortunately, it has to be custom.
Any help will be appreciated.
standard__namedPage
but you are usingstandard__recordPage
.Utility.getAttachmentFiles
method? It should be ContentDocumentLink.