Skip to main content The Trailblazer Community will be unavailable from 2/1/2025 to 2/2/2025. Please plan your activities accordingly.
APEX  Class>>>>>>>>>

public with sharing class AccountManager {

   

    @AuraEnabled(cacheable=true)

    public static list<Account> getAccounts(){

           

       list<Account> listAcc = [SELECT Id,Name,Rating,AnnualRevenue from Account LIMIT 8];

       return listAcc;

    }

    @AuraEnabled

    public static void deleteselectedRecords(list<Id> listOfIds){

        list<Account>  listOfAcc = [SELECT Id,Name,Rating,AnnualRevenue from Account where ID IN : listOfIds];

        if(!listOfAcc.isEmpty()){

            delete listOfAcc;

        }

    }

}

html>>>>>>>>>>>>>>>>>>

<template>

    <lightning-card title="Delete selected Rows">

        <lightning-datatable

        key-field="id"

        data={getDetials}

        columns={columns} onrowselection={onselctedRowHandler}

        >

           

        </lightning-datatable>

        <div class="slds-m-around_medium">

            <lightning-button label="DeleteSelectedRecords" variant="brand" onclick={ClickDeleteRecords}></lightning-button>

        </div>

       

    </lightning-card>

</template>

import { LightningElement,wire } from 'lwc';

import getAccounts from '@salesforce/apex/AccountManager.getAccounts'

import deleteselectedRecords from '@salesforce/apex/AccountManager.deleteselectedRecords'

import {ShowToastEvent} from 'lightning/platformShowToastEvent';

import { refreshApex } from '@salesforce/apex';

export default class DeleteSelectedRows extends LightningElement {

getDetials

columns =[

    {label:'Id',fieldName:'Id'},

    {label:'AccName',fieldName:'Name'},

    {label:'Rating',fieldName:'Rating'},

    {label:'AnnualRevenue',fieldName:'AnnualRevenue'}

];

@wire (getAccounts) getAccdetails({data,error}){

if(data){

    this.getDetials = data;

     console.log(data)

     console.log(this.getDetials)

}

else{

     console.log(error)

}

}

seletedRowIdList =[]

onselctedRowHandler(event){

    const seletedRows = event.detail.selectedRows

   console.log(seletedRows)

   for(let i=0; i< seletedRows.length; i++){

    this.seletedRowIdList.push(seletedRows[i].Id)

    console.log(this.seletedRowIdList)

   }

}

ClickDeleteRecords(){

    deleteselectedRecords({listOfIds : this.seletedRowIdList}).then(()=>

    {

        const toastEvent = new ShowToastEvent({

            title:'Success!',

            message:'Record deleted successfully',

            variant:'success'

          });

          this.dispatchEvent(toastEvent);

          return refreshApex(this.getAccdetails);

    }

    ).catch((error)=>{

        console.log(error)

    }

    );

}

}

 
1 answer
0/9000