Skip to main content

Hi, 

 

I have a LWC that gets the files from a record and displays them with a download link, I want to update this so the "Download" link is a "Download" button that does the same actions but also the abillity to run a flow when the "Download" Button is clicked too.

 

My flow has a number of input varibale to pass data in, I can get a lot of the values from the flow my LWC sits in, but not sure how I can get the FIle name/details that has been downloaded to pass through?

 

My LWC:

 

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_6-of-12 slds-medium-size_4-of-12 slds-size_1-of-12">

                       <p style="font-size: large;"><strong>FileName - </strong>{file.label}</p>

                     </div>

                     <div class="slds-col slds-large-size_6-of-12 slds-medium-size_4-of-12 slds-size_1-of-12">

                       <a style="font-size: large;" 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>

JS:

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) {

//pass recordId from target in meta-xml 

    @api recordId

    @api userId

    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,

             //redirect to new portal site, update "procure" with new name

             "url":`/SiteName/sfsites/c/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" />

            <property name="userId" type="String" label="User 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

 

@Lightning Web Components@Lightning Components Development

9 answers
  1. Dec 11, 2024, 1:28 PM

    Hi Callum,

     

    To get the logged in userid you can use this: 

    import Id from '@salesforce/user/Id';

    export default class MiscGetUserId extends LightningElement {

    userId = Id;

    }

    to get the file label, you'll need to use the dataset to get the data attribute:

    handledownload(event){

    this.createAuditLog(event);

    }

    createAuditLog(event){

    console.log('test123' + this.accountName + " file downloaded by "+ this.userId + this.recordId + this.item)

    const fields={};

    fields[AUDIT_TITLE.fieldApiName] = "File Downloaded";

    fields[PARENTID.fieldApiName] = this.recordId;

    fields[PARENTOBJECT.fieldApiName] = "NL_Tender_Opportunity__c";

    fields[AUDIT_DETAILS.fieldApiName] = event.target.dataset.filename +"file downloaded by "+ this.accountName;

    fields[USER_FIELD.fieldApiName] = this.userId;

    this blog explains how to pass data from a Flow to your LWC. https://salesforcediaries.com/2023/01/08/flow-to-lwc-pass-data-instantly/

     

    It looks like you have the structure for that set so perhaps there is an issue within your Flow. Anyway, you can get the recordId and AccountName from there.

0/9000