0

How to display a preview of an attached pdf file in a new (this is better) or old window using the Apex handler class in LWC. LWC is connected to quick action in salesforce object. As a result, I get no reaction, even if I insert the ContentDocumentId, which I took from salesforce, directly into the selectedRecordId. I've tried different ways, including using the Apex handler class, but nothing helps. Maybe I didn't get some data? I changed the text of the question and the code to make it clearer and I have removed the code for other functions that works fine.

I did according to https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.use_open_files , and I saw this post - How to preview the file attached in the ContentVersion - nothing helps.

html file:

<template>
    <lightning-card title="Send Invoice" >

        <a onclick={navigateToFiles}>Navigate To Files</a>

    </lightning-card>
</template>

js-meta.xml file

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordAction</target>
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordAction">
            <actionType>ScreenAction</actionType>
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>

js file

import { LightningElement, api, wire, track } from 'lwc';
import { getSObjectValue,refreshApex} from '@salesforce/apex';
import { CloseActionScreenEvent } from 'lightning/actions';
// importing navigation service
import { NavigationMixin } from 'lightning/navigation';

export default class sendInvoiceLWC2 extends LightningElement {
    
    navigateToFiles() {
      this[NavigationMixin.Navigate]({
        type: 'standard__namedPage',
        attributes: {
            pageName: 'filePreview'
        },
        state : {
            selectedRecordId:'0695j0000035a8SAAQ'
        }
      })
    }
}

apex handler class which I tried to use and it is not used in this example

public with sharing class SendInvoiceService {
    @AuraEnabled(cacheable=true)
    public static List<ContentVersion> getPreview(id contentVId){
        return [SELECT Id, Title, FileExtension, ContentDocumentId From ContentVersion where id=:contentVId];
    }
...
}
1

1 Answer 1

1

Please change your component from extends LightningElement to extends NavigationMixin(LightningElement).

E.g.

import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class sendInvoiceLWC2 extends NavigationMixin(LightningElement) {
    
    navigateToFiles() {
      this[NavigationMixin.Navigate]({
        type: 'standard__namedPage',
        attributes: {
            pageName: 'filePreview'
        },
        state : {
            selectedRecordId:'0695j0000035a8SAAQ'
        }
      })
    }
}

Note: this will redirect to a hard-coded record id, but then you can extend to navigate to the desired record, you can continue using the structure from your mentioned reference answer.

2
  • Thanks a lot, I missed this little thing Nov 24, 2021 at 15:06
  • I know, I did the same while playing with NavigationMixin and that is why I kept it in my head. Nov 24, 2021 at 15:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .