1

I'm trying to get a list of all CaseT records under their respective recordtype name in a lwc component.

So, CaseT Recordtypename 1

  1. CaseT A
  2. CaseT B
  3. CaseT C

CaseT Recordtypename 2

  1. CaseT X
  2. CaseT Y
  3. CaseT Z

I have tried the following:

APEX:

@AuraEnabled
public static Map<string, List<CaseT__c>> caseTrxMap(Id caseId){
    case caseRec = getCaseConsumer(caseId);
    String consumerId ='';

    if(caseRec != null ){
        consumerId = caseRec.AccountId;
        System.debug('@@consumerId:: '+consumerId);
    }

    Map<String, List<CaseT__c>> mapObj = new Map<String, List<CaseT__c>>();        
    List<RecordType> rTList = [SELECT Name, Id FROM RecordType 
                               where SobjectType ='CaseT__c'];

    for (CaseT__c transaxItem : [SELECT RecordType.Name, ConsumerName__c 
                                                  FROM CaseT__c 
                                                  where ConsumerName__c =:consumerId]) {            

        if(!mapObj.containsKey(transaxItem.RecordType.Name)) {
             mapObj.put(transaxItem.RecordType.Name, new List<CaseT__c>());
               mapObj.get(transaxItem.RecordType.Name).add(transaxItem);
        }
        else {
           mapObj.get(transaxItem.RecordType.Name).add(transaxItem);
        }            
    }
    system.debug('mapObj'+mapObj);
    return mapObj;
}

Here's the JS controller:

import { LightningElement, wire, track } from 'lwc';
import caseList from '@salesforce/apex/caseTrxMap.caseTrxMap';

export default class myLWC extends LightningElement {
@track accounts;
@track error;

@wire(caseList)
wiredAccounts({ error, data }) {
    if (data) {
        this.accounts = data;
        this.error = undefined;
    } else if (error) {
        this.error = error;
        this.accounts = undefined;
    }
}

}

Here's the HTML:

<template>
    <template if:true={accounts}>
        <template for:each={accounts} for:item="account">
               <p key={account.Id}>Account - {account.Name}</p>
        </template>
   </template>
 </template>

I'm able to get the recordtype names and values in the map of maps (Map>), but I'm at a loss as to how the wiring works (@wire: a small description would be great). I'm unable to put the values on the HTML (planning on Accordion, trying my luck)

Kindly point me to the right direction

2
  1. Your method should have (Cacheable = true)

"If an Apex method is annotated with @AuraEnabled(cacheable=true), you can invoke it from a component via the wire service."

    @AuraEnabled(Cacheable = true)
  1. You should pass caseId

    @track caseId;

    @wire(caseList, { caseId: '$caseId' }) wiredAccounts({ error, data }) { if (data) { this.accounts = data; this.error = undefined; } else if (error) { this.error = error; this.accounts = undefined; } }

  2. You cannot iterate through the map using

    template for:each={accounts} It works only for List, Array, not for Map.

    To iterate by specific List you need to do something like that:

    template for:each={accounts.YourRecordTypeName} it allows you to iterate only by one record Type.

Here is my proposition. Create a wrapper instead of map and return it in your method.

APEX

public class RecordTypeWrapper {
    @AuraEnable
    public String recordTypeName;

    @AuraEnable
    public List<CaseT__c> cases;

    public RecordTypeWrapper(String recordTypeName, List<CaseT__c> cases) {
       this.recordTypeName = recordTypeName;
       this.cases = cases;
    }
}


@AuraEnabled(Cacheable=true)
public static List<RecordTypeWrapper> caseTrxMap(Id caseId){
    case caseRec = getCaseConsumer(caseId);
    String consumerId ='';

    if(caseRec != null ){
       consumerId = caseRec.AccountId;
       System.debug('@@consumerId:: '+consumerId);
    }

    List<RecordTypeWrapper> recordTypesList = new List<RecordTypeWrapper>();

    Map<Id, RecordType> recordsTypeMap = new Map<Id, RecordType>([SELECT Name, Id FROM RecordType 
                                                                  WHERE SobjectType ='CaseT__c'])

    List<CaseT__c> allCasesList = [ SELECT RecordType.Name, ConsumerName__c 
                                    FROM CaseT__c 
                                    WHERE BC_ConsumerName__c =:consumerId ];
    List<CaseT__c> casesForRecordType;
    for (Id recordTypeId : recordsTypeMap.keyset()) {

        casesForRecordType = new List<CaseT__c>();//reset

        for (CaseT__c case : allCasesList) {
            if (case.RecordType.Name == recordsTypeMap.get(recordTypeId).Name) {
                  casesForRecordType.add(case);
            }                 
        }

        recordTypesList.add(
            new RecordTypeWrapper(
               recordsTypeMap.get(recordTypeId).Name,
               casesForRecordType
           )
       );

    }

    return recordTypesList;
}

LWC:

<template for:each={accounts} for:item="account">
     {account.recordTypeName}
     <template for:each={account.cases} for:item="case">
         {case.ConsumerName__c} 
     </temlate>
</template>

Wire description here.

| improve this answer | |
  • Thank you for the wonderful solution. Records were easier to display with wrapper than with the map directly. – MnZ Feb 19 at 9:19
  • 1
    I am glad that I could help! :D – Piotr Gajek Feb 19 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.