1

In my LWC I am using an Apex Class to return a Map<String,List> containing lists for Leads and Contacts.
I want to iterate this Map and use the individual lists in separate datatables.
I have this working when I configure the apex class to return just the lead List. However I cannot figure out the syntax for iterating the Map and returning the specific lists based on the Key. Below is the code

LWC .js

    leadData = [];
    leadWiredList;
    count = 0;
    @wire(getCampaignMemberList, {
        campaignId: '$campaignId',
        leadSearchKey: '$searchKey', 
        leadSortBy: '$sortedBy', 
        leadSortDirection: '$sortedDirection'
    })
    wiredLeads(leadResponse){
        this.leadWiredList = leadResponse;
        const error = leadResponse.error;
        if(leadResponse.data){
            let tempList = []; 
            leadResponse.data.forEach((record) => {
            let r = Object.assign({}, record);  
                r.LeadLink = '/' + r.Id;
                r.SdrOwner = r.SDR_Owner__r?.Name;
            tempList.push(r);             
            });          
        this.leadData = tempList;
            console.log('Leads ' + this.leadData.length);
        }else{
            this.leadData = [];
        }   
    }

Apex

@AuraEnabled(cacheable=true)
    public static Map<String,List<Sobject>> getLeadsContacts(
    //public static List<Sobject> getLeadsContacts(
        String campaignId, 
        String leadSearchKey, 
        String leadSortBy, 
        String leadSortDirection
    ){
        Map<String,List<Sobject>> leadsContactsMap = new Map<String,List<Sobject>>();
        List<CampaignMember> campaignMembers = [
            SELECT Id, Campaign.Name, LeadId, ContactId
            FROM CampaignMember
            WHERE CampaignId = :campaignId
            AND (
                Lead.OwnerId = :runningUserId
                OR Contact.OwnerId = :runningUserId
            )
        ];

        List<String> leadIds = new List<String>();
        List<String> contactIds = new List<String>();
        for(CampaignMember cm : campaignMembers){
            if(cm.LeadId != null){
                leadIds.add('\'' + cm.LeadId + '\'');
            }else{
                contactIds.add('\'' + cm.ContactId + '\'');
            }     
        }

        String leadQuery = 
            'SELECT Id,'
            + 'Lead_Status_sort_order__c, SDR_Owner__r.Name, Name, email, Title, Company '
            + 'FROM Lead '
            + 'WHERE Id IN ' + leadIds;
        if(leadSearchKey != null && leadSearchKey != '') {
            String key = '%' + leadSearchKey + '%';
            leadQuery += 
            ' AND Name LIKE :key';
        }
        if(leadSortBy != null && leadSortDirection != null) {
            leadQuery += ' ORDER BY ' + leadSortBy + ' ' + leadSortDirection;
        }
            system.debug('Lead Query = ' + leadQuery);
        List<Lead> leadlist = Database.query(leadQuery);

       List<Contact> contactList = [
            SELECT Id, Name, Email
            FROM Contact
            WHERE ID IN :contactIds
        ];

        leadsContactsMap.put('leads', leadlist);
        leadsContactsMap.put('contacts', contactList);
            system.debug('leadsContactsMap = ' + leadsContactsMap);

      return leadsContactsMap;
  }

1 Answer 1

0

To work with apex maps in lwc, You can go with this approach:-

for (var key in leadResponse.data) {
    console.log('key', key);
    console.log('value',leadResponse.data[key]);
}

Inside the iteration, leadResponse.data[key] will have list of sObject which you are passing from your apex method.

and key will be the map key for each key value pair available in your response.

Read more here:

  1. https://salesforcediaries.com/2022/07/17/iterate-apex-map-in-lightning-web-component/
  2. How to get values of map passed form apex to lwc js?

You must log in to answer this question.

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