LWC "Printable View" that prints the current page layout of Object A along with report data,





<template>
    <lightning-card title="Printable View - {record.fields.Name.value}">
        <div if:true={record}>
            <!-- Display the record details -->
            <p>Field 1: {record.fields.Field1__c.value}</p>
            <p>Field 2: {record.fields.Field2__c.value}</p>
            <!-- Add more fields as needed -->
        </div>

        <hr/>

        <!-- Display report data in a table format -->
        <template if:true={reportData}>
            <h2>Report Data</h2>
            <table class="slds-table slds-table_cell-buffer slds-table_bordered">
                <thead>
                    <tr>
                        <th scope="col">Field 1</th>
                        <th scope="col">Field 2</th>
                        <!-- Add more columns as necessary -->
                    </tr>
                </thead>
                <tbody>
                    <template for:each={reportData} for:item="report">
                        <tr key={report.Id}>
                            <td>{report.Field_1__c}</td>
                            <td>{report.Field_2__c}</td>
                            <!-- Add more fields as necessary -->
                        </tr>
                    </template>
                </tbody>
            </table>
        </template>

        <lightning-button label="Print" onclick={handlePrint} class="slds-m-top_medium"></lightning-button>
    </lightning-card>
</template>

import { LightningElement, api, wire } from 'lwc';
import { getRecord } from 'lightning/uiRecordApi';
import getReportData from '@salesforce/apex/PrintableViewController.getReportData';

// Specify the fields to retrieve from Object A
const FIELDS = [
    'Object_A__c.Field1__c',
    'Object_A__c.Field2__c',
    // Add more fields as needed
];

export default class PrintableView extends LightningElement {
    @api recordId;
    record;
    reportData;

    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })
    wiredRecord({ error, data }) {
        if (data) {
            this.record = data;
        } else if (error) {
            console.error(error);
        }
    }

    @wire(getReportData, { recordId: '$recordId' })
    wiredReportData({ error, data }) {
        if (data) {
            this.reportData = data;
        } else if (error) {
            console.error(error);
        }
    }

    handlePrint() {
        window.print();
    }
}



public with sharing class PrintableViewController {
    @AuraEnabled(cacheable=true)
    public static List getReportData(Id recordId) {
        return [SELECT Field_1__c, Field_2__c FROM ReportObject__c WHERE Object_A__c = :recordId];
    }
}


LWC "Printable View" that prints the current page layout of Object A along with report data, LWC "Printable View" that prints the current page layout of Object A along with report data, Reviewed by dasfrogpractice on 02:38 Rating: 5

No comments:

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe