1

ShowAcct.cls

public with sharing class ShowAcct {
    @AuraEnabled(Cacheable=true)
    public static List<Account> getAccountList() {
        return [SELECT id,name,AnnualRevenue from Account];
    }
}

showAcctRecordswithCheckbox.html

<template>
    <lightning-input type="checkbox"
                     label="Show/Hide"
                     checked={showFields}
                     onchange={toggleFields}>
    </lightning-input>
    <template if:true={showFields}>
        <template if:true={accList}>
            <lightning-datatable 
                    data ={acctList}  
                    columns={columns} 
                    key-field="id" >
            </lightning-datatable>  
        </template>
    </template>    
    <template if:true={error}>
        {error}
    </template>
</template>

showAcctRecordswithCheckbox.html

import { LightningElement,api,wire,track} from 'lwc';
import getAccountList from '@salesforce/apex/ShowAcct.getAccountList';

export default class ShowAcctRecordswithCheckbox extends LightningElement {
    showFields = true;
    @track columns;
    
    toggleFields() {
        this.showFields = !this.showFields;
        columns=[{
            label:'Account Name',
            fieldName: 'Name',
            type:'text',
            sortable:true
        },{
            label:'Annual Revenue',
            type:'Currency',
            fieldName:'Annual Revenue',
            sortable:true
        }];
    }

    @track accList;
    
    @wire(getAccountList)
    wiredAccounts({error,data}) {
        if(data){
            this.accList = data;
        } else if(error){
            this.error = error;
        }
    }
}

Class is successfully deployed but it shows only checked checkbox ...no fields and as soon as i unchecked it it shows error SORRY TO INTURUPPT

This page has an error. You might just need to refresh it. First, would you give us some details? (We're reporting this as error ID: -465312255)

1 Answer 1

1

Firstly I found typos in code:

  • data = {acctList} and in js code: accList
  • fieldName: "Annual Revenue", should be fieldName: "AnnualRevenue"

Additionally you not need @track decorator as you reassign whole objects.

Corrected code:

showAcctRecordswithCheckbox.html

<template>
    <lightning-input type="checkbox" 
                     label="Show/Hide" 
                     checked={showFields} 
                     onchange={toggleFields}> 
    </lightning-input>
    <template if:true={showFields}>
        <template if:true={accList}>
            <lightning-datatable data={accList} 
                                 columns={columns} 
                                 key-field="id">
            </lightning-datatable>
        </template>
    </template>
    <template if:true={error}> {error} </template>
</template>

showAcctRecordswithCheckbox.js

import { LightningElement, wire } from "lwc";
import getAccountList from "@salesforce/apex/ShowAcct.getAccountList";

export default class ShowAcctRecordswithCheckbox extends LightningElement {
    accList;
    showFields = true;
    columns = [
        {
            label: "Account Name",
            fieldName: "Name",
            type: "text",
            sortable: true
        },
        {
            label: "Annual Revenue",
            type: "Currency",
            fieldName: "AnnualRevenue",
            sortable: true
        }
    ];

    toggleFields() {
        this.showFields = !this.showFields;
    }

    @wire(getAccountList)
    wiredAccounts({ error, data }) {
        if (data) {
            this.accList = data;
        } else if (error) {
            this.error = error;
        }
    }
}

1
  • Thank You So Much Commented Apr 25, 2021 at 11:00

You must log in to answer this question.

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