Tuesday, March 8, 2022

LWC Display List of Cases in Table

 HTML : 

<!--

 * @File Name          : LWC_CaseLWCService.cls

 * @Description        : 

 * @Author             : Venkataramana D

 * @Group              : 

 * @Last Modified By   : Venkataramana D

 * @Last Modified On   : 28/FEB/2022, 1:54:34 pm

 * @Modification Log   : 

 * Ver       Date            Author          Modification

 * 1.0    28/FEB/2022   Venkataramana D     Initial Version

-->

<template>

    Testing:

    <lightning-card  variant="Narrow"  title="Case List" icon-name="standard:case">

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

            <lightning-datatable

                key-field="id"

                data={result}

                show-row-number-column

                hide-checkbox-column

                columns={columnsList}

                onrowaction={handleRowAction}>

            </lightning-datatable>

        </div>

    </lightning-card>

</template>

----------------------------------------------------------------------------------------------------

JS:

import { LightningElement, wire, api, track } from 'lwc';

import fetchCases from '@salesforce/apex/LWC_CaseLWCService.fetchCases';


const columns = [

    { 

        label: 'CaseNumber', fieldName: 'caseUrl', type:'url',

        typeAttributes: {

            label: { 

                fieldName: 'CaseNumber' 

            },

            target : '_blank'

        }

    },

    { 

        label: 'Subject', fieldName: 'subjectUrl', wrapText: true,

        type: 'url',

        typeAttributes: {

            label: { 

                fieldName: 'Subject' 

            },

            target : '_blank'

        }

    },

    { label: 'Status', fieldName: 'Status' },

    {

        label: 'Priority', fieldName: 'Priority',

        cellAttributes:{ 

            iconName: { 

                fieldName: 'priorityIcon' 

            },

            iconPosition: 'left', 

            iconAlternativeText: 'Priority Icon' 

        }

    },

    { 

        label: 'Contact', fieldName: 'ContactUrl', wrapText: true,

        type: 'url',

        typeAttributes: {

            label: { 

                fieldName: 'ContactName' 

            },

            target : '_blank'

        } 

    },

    { 

        label: 'Account', fieldName: 'AccountUrl', wrapText: true,

        type: 'url',

        typeAttributes: {

            label: { 

                fieldName: 'AccountName' 

            },

            target : '_blank'

        } 

    }

];



            


export default class LWC_CaseDatatable extends LightningElement {

    

    @api result;

    @track error;


    columnsList = columns;

    

    connectedCallback(){

        this.getAllCaseDetails();

    }


    getAllCaseDetails(){

        fetchCases()

            .then(data => {

                /* Iterate with Each record and check if the Case is Associated with Account or Contact

                    then get the Name and display into datatable

                */

                /* Prepare the Org Host */

                let baseUrl = 'https://'+location.host+'/';

                data.forEach(caseRec => {

                    caseRec.caseUrl = baseUrl+caseRec.Id;

                    if(caseRec.Subject==null){

                        caseRec.subjectUrl  = null;

                    }else {

                        caseRec.subjectUrl = baseUrl+caseRec.Id;

                    }

                    if(caseRec.ContactId){

                        caseRec.ContactName = caseRec.Contact.Name;

                        /* Prepare Contact Detail Page Url */

                        caseRec.ContactUrl = baseUrl+caseRec.ContactId;

                    }

                    if(caseRec.AccountId){

                        caseRec.AccountName = caseRec.Account.Name;

                        /* Prepare Account Detail Page Url */

                        caseRec.AccountUrl = baseUrl+caseRec.AccountId;

                    }


                    if(caseRec.Priority === 'High'){

                        caseRec.priorityIcon = 'utility:log_a_call';

                    } else if (caseRec.Priority === 'Medium'){

                        caseRec.priorityIcon = 'utility:note';

                    } else if(caseRec.Priority === 'Low'){

                        caseRec.priorityIcon = 'utility:open';

                    }

                });

                this.result = data;

                window.console.log(' data ', data);

                this.error = undefined;

            })

            .catch(error => {

                this.error = error;

                window.console.log(' error ', error);

                this.result = undefined;

            });

    }

    

    handleRowAction(){

        

    }

}

-----------------------------------------------------------------------------------------------------

Meta.XML

<?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__AppPage</target>

        <target>lightning__RecordPage</target>

        <target>lightning__HomePage</target>

      </targets>

</LightningComponentBundle>

-------------------------------------------------------------------------------------------------------

APex Class :

/**

 * @File Name          : CaseLWCService.cls

 * @Description        : 

 * @Author             : Venkataramana D

 * @Group              : 

 * @Last Modified By   : Venkataramana D

 * @Last Modified On   : 28/FEB/2022, 1:54:34 pm

 * @Modification Log   : 

 * Ver       Date            Author          Modification

 * 1.0    28/FEB/2022   Venkataramana D     Initial Version

**/

public with sharing class LWC_CaseLWCService {

    public LWC_CaseLWCService() {


    }


    @AuraEnabled

    public static List<Case> fetchCases(){

        return [SELECT Id, CaseNumber, Subject, Description, AccountId, Account.Name, ContactId,

                Contact.Name, Status, Priority FROM CASE LIMIT 10];

    }

}