Hi,
I have used an LWC from https://www.salesforcetroop.com/file_preview_lwc to develop a File download LWC to get the related Files of a recordId that I pass through in a Screen flow and Dispaly the LWC in a Screen flow sitting on my Experince Site, when I test it, it works in debug/internally. I want to allow it to work on my Community portal, every one who has access is a logged into the site, but when ever I download I get a JSON file with top.location='https://domain-name.sandbox.my.site.com/ex/errorduringprocessing.jsp'
My portal is structured "https://domainname.sandbox.my.site.com/portalname/s/"
The URL would be on a record "https://domainname.sandbox.my.site.com/portalname/s/Objectname/recordId/toName"
the LWC I have used is
HTML:
<template>
<lightning-card title="Documentation">
<template for:each={filesList} for:item="file">
<div key={file.value} class="slds-box">
<div class="slds-grid slds-wrap">
<div class="slds-col slds-large-size_4-of-12 slds-medium-size_4-of-12 slds-size_12-of-12">
<p><strong>FileName - </strong>{file.label}</p>
</div>
<div class="slds-col slds-large-size_4-of-12 slds-medium-size_4-of-12 slds-size_12-of-12">
<a href={file.url} download>Download</a>
</div>
<!--remove Preview button-->
<!--
<div class="slds-col slds-large-size_4-of-12 slds-medium-size_4-of-12 slds-size_12-of-12">
<lightning-button label="Preview"
variant="brand"
data-id={file.value}
onclick={previewHandler}
></lightning-button>
</div>-->
</div>
</div>
</template>
</lightning-card>
</template>
Javascript:
import { LightningElement, api, wire } from 'lwc';
import getRelatedFilesByRecordId from '@salesforce/apex/filePreviewAndDownloadController.getRelatedFilesByRecordId'
import {NavigationMixin} from 'lightning/navigation'
export default class FilePreviewAndDownloads extends NavigationMixin(LightningElement) {
@api recordId;
filesList =[]
@wire(getRelatedFilesByRecordId, {recordId: '$recordId'})
wiredResult({data, error}){
if(data){
console.log(data)
this.filesList = Object.keys(data).map(item=>({"label":data[item],
"value": item,
"url":`/sfc/servlet.shepherd/document/download/${item}`
}))
console.log(this.filesList)
}
if(error){
console.log(error)
}
}
previewHandler(event){
console.log(event.target.dataset.id)
this[NavigationMixin.Navigate]({
type:'standard__namedPage',
attributes:{
pageName:'filePreview'
},
state:{
selectedRecordId: event.target.dataset.id
}
})
}
}
Meta-XML:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>62.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
<target>lightning__FlowScreen</target>
<target>lightningCommunity__Page</target>
<target>lightningCommunity__Page_Layout</target>
</targets>
<!--enable passing in a recordId via screen flow varriable-->
<targetConfigs>
<targetConfig targets="lightning__FlowScreen">
<property name="recordId" type="String" label="Input ID" />
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
CLS:
public with sharing class filePreviewAndDownloadController {
@AuraEnabled(cacheable=true)
public static Map<ID, String> getRelatedFilesByRecordId(String recordId) {
// Get record file IDs
List<ContentDocumentLink> files = [SELECT ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId = :recordId];
List<ID> fileIDs = new List<ID>();
for (ContentDocumentLink docLink : files) {
fileIDs.add(docLink.ContentDocumentId);
}
List<ContentVersion> docs = [SELECT ContentDocumentId, FileExtension, Title
FROM ContentVersion WHERE ContentDocumentId IN : fileIDs];
Map<ID, String> mapIdTitle = new Map<ID, String>();
for (ContentVersion docLink : docs) {
mapIdTitle.put(docLink.ContentDocumentId, docLink.Title);
}
return mapIdTitle;
}
}
Thanks
@* Salesforce Developers *@Salesforce Flow Automation@Lightning Web Components@Lightning Components Development
Hi @Callum North.
Could you please try updating the file download URL in your LWC to include the correct community path, as suggested in the following thread:
https://salesforce.stackexchange.com/questions/203030/content-document-download-url-params
Hope this helps. Thanks!