function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rafael Franco Moreno 24Rafael Franco Moreno 24 

close cases

how can I close cases selected in an LWC? I need to close cases if they don´t have a Reason for closure(custom field), I don´t find anything of code, any idea? I'm newbie

User-added image

this is my html:

<template>
        <lightning-card title="Inline Edit With Lightning Datatable in LWC">
            <template if:true={cases.data}>
                <lightning-datatable key-field="Id"
                                     data={cases.data}
                                     columns={columns}
                                     onsave={handleSave}
                                     draft-values={saveDraftValues}                                                                show-row-number-column
                                     onrowselection={getSelectedName}>
                </lightning-datatable>
            </template>
        </lightning-card>
    </template>

this is js

import { LightningElement, wire, track } from 'lwc';
import getCases from '@salesforce/apex/getRecordDataController.getCases';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { refreshApex } from '@salesforce/apex';
// datatable columns
const columns = [
    {
        label: 'CaseNumber',
        fieldName: 'CaseNumber',
        type: 'text',
    }, {
        label: 'Subject',
        fieldName: 'Subject',
        type: 'text',
        editable: true,
    }, {
        label: 'Status',
        fieldName: 'Status',
        type: 'text',
        editable: true,
    }, {
        label: 'reason_for_closure__c',
        fieldName: 'reason_for_closure__c',
        type: 'phone',
        editable: true
    }
];
export default class InlineEditTable extends LightningElement {
    columns = columns;
    @track cases;
    saveDraftValues = [];
    @wire(getCases)
    cons(result) {
        this.cases = result;
        if (result.error) {
            this.cases = undefined;
        }
    };
    getSelectedName(event) {
     const selectedRows = event.detail.selectedRows;
     // Display that fieldName of the selected rows
     for (let i = 0; i < selectedRows.length; i++){
         console.log("You selected: " + selectedRows[i].CaseNumber);
     }
 }
    handleSave(event) {
        this.saveDraftValues = event.detail.draftValues;
        const recordInputs = this.saveDraftValues.slice().map(draft => {
            const fields = Object.assign({}, draft);
            return { fields };
        });
        // Updateing the records using the UiRecordAPi
        const promises = recordInputs.map(recordInput => updateRecord(recordInput));
        Promise.all(promises).then(res => {
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Success',
                    message: 'Records Updated Successfully!!',
                    variant: 'success'
                })
            );
            this.saveDraftValues = [];
            return this.refresh();
        }).catch(error => {
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error',
                    message: 'An Error Occured!!',
                    variant: 'error'
                })
            );
        }).finally(() => {
            this.saveDraftValues = [];
        });
    }
    // This function is used to refresh the table once data updated
    async refresh() {
        await refreshApex(this.cases);
    }
}

this is the class:

public with sharing class getRecordDataController {
    //@AuraEnabled is annotation enables LWC to access below apex method
    //(cacheable=true) is for caching the data on client side storage without
      //waiting for server trips. Which imporves the performance
    @AuraEnabled(cacheable=true)
     public static List<Case> getCases() {
     return [SELECT CaseNumber, Subject, Status, reason_for_closure__c FROM Case WHERE Status = 'New'];
     }
   }
mukesh guptamukesh gupta
Hi Rafeal,

Please use below code:-

Apex class:-
 
public with sharing class getRecordDataController {
    //@AuraEnabled is annotation enables LWC to access below apex method
    //(cacheable=true) is for caching the data on client side storage without
      //waiting for server trips. Which imporves the performance
    @AuraEnabled(cacheable=true)
     public static List<Case> getCases() {
     return [SELECT Id,CaseNumber, Subject, Status, dotsquaremukesh__reason_for_closure__c FROM Case WHERE Status = 'New' AND dotsquaremukesh__reason_for_closure__c = ''];
     }

     @AuraEnabled
     public static List<Case>  updateStatus(List<String> caseIds){
         List<Case> updateCase  = new List<Case>();
        List<Case> caseList = [SELECT Id,CaseNumber, Subject, Status FROM Case Where Id IN: caseIds];
        for(Case c : caseList){
            c.Status = 'Closed';
            updateCase.add(c);
        } 

        if(updateCase.size() > 0)
          update updateCase;

        return updateCase;
     }

   }

.html
 
<template>

    Case Close List
    <lightning-card title="Inline Edit With Lightning Datatable in LWC">
        <lightning-button onclick={updateCaseStatus} label="Close Selected Case"></lightning-button>
        <template if:true={cases.data}>
            <lightning-datatable key-field="Id"
                                 data={cases.data}
                                 columns={columns}
                                 onsave={handleSave}
                                 draft-values={saveDraftValues}                                                            
                                 onrowselection={getSelectedName}>
            </lightning-datatable>
        </template>
    </lightning-card>
</template>
 
import { LightningElement, wire, track } from 'lwc';
import getCases from '@salesforce/apex/getRecordDataController.getCases';
import updateCaseStatus from '@salesforce/apex/getRecordDataController.updateStatus';
import { updateRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { refreshApex } from '@salesforce/apex';
// datatable columns
const columns = [
    {
        label: 'CaseNumber',
        fieldName: 'CaseNumber',
        type: 'text',
    }, {
        label: 'Subject',
        fieldName: 'Subject',
        type: 'text',
        editable: true,
    }, {
        label: 'Status',
        fieldName: 'Status',
        type: 'text',
        editable: true,
    }, {
        label: 'Reason for closure',
        fieldName: 'dotsquaremukesh__reason_for_closure__c',
        type: 'text',
        editable: true
    }
];
export default class CaseClose extends LightningElement {
    columns = columns;
    @track cases;
    @track error;
    saveDraftValues = [];
    selectedCase = [];

    @wire(getCases)
    cons(result) {
        this.cases = result;
        if (result.error) {
            this.cases = undefined;
        }
    };
    getSelectedName(event) {
     const selectedRows = event.detail.selectedRows;
     // Display that fieldName of the selected rows
     for (let i = 0; i < selectedRows.length; i++){
         //console.log("You selected: " + selectedRows[i].CaseNumber);
         this.selectedCase.push(selectedRows[i].Id);
     }
 }
   /* handleSave(event) {
        this.saveDraftValues = event.detail.draftValues;
        const recordInputs = this.saveDraftValues.slice().map(draft => {
            const fields = Object.assign({}, draft);
            return { fields };
        });
        // Updateing the records using the UiRecordAPi
        const promises = recordInputs.map(recordInput => updateRecord(recordInput));
        Promise.all(promises).then(res => {
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Success',
                    message: 'Records Updated Successfully!!',
                    variant: 'success'
                })
            );
            this.saveDraftValues = [];
            return this.refresh();
        }).catch(error => {
            this.dispatchEvent(
                new ShowToastEvent({
                    title: 'Error',
                    message: 'An Error Occured!!',
                    variant: 'error'
                })
            );
        }).finally(() => {
            this.saveDraftValues = [];
        });
    }*/

    updateCaseStatus(){
           console.log('selectedCase-- >>> '+this.selectedCase);
           updateCaseStatus({ caseIds: this.selectedCase })
           .then(result => {
            console.log('OK RESULT==>>> '+JSON.stringify(result));
           // console.log('KO ERRRO==>>> '+this.error);
               this.cases = result;
               this.error = undefined;
           })
           .catch(error => {
               this.error = error;
               this.cases = undefined;
           })
           
        }
    

    }

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Rafael Franco Moreno 24Rafael Franco Moreno 24
Thank you Mukesh it works but I had a mistake, cases must be closed if they have a reason for closure only, I modified this method but it doesn't work when the field reason for the closure has the text:
 
@AuraEnabled
     public static List<Case>  updateStatus(List<String> caseIds){
         List<Case> updateCase  = new List<Case>();
        List<Case> caseList = [SELECT Id,CaseNumber, Subject, Status, reason_for_closure__c FROM Case Where Id IN: caseIds];
        for(Case c : caseList){
          if (c.reason_for_closure__c != null) {
            c.Status = 'Closed';
            updateCase.add(c);
          }            
        }

then when I enter text in the reason for closure field cancel and save buttons appears:

User-added image

and when I hit save show this error: 

User-added image

I'm trying to hide buttons with css but I get this error:

User-added image

I don´t know very well the way to hide that buttons with css, or How can I do it?

thanks in advance