1

I have below piece of code where I want to traverse to details from the base query on master object.

Apex code

Map<Id,set<Id>> MapOfAcctListItem = new Map<Id,Set<Id>>();

for(Account_List_vod__c cpc : [select Access_vod__c,Display_Order_vod__c,Icon_Name_vod__c,Id,Name,OwnerId, 
                                        (select ID,Account_vod__c,External_ID_vod__c 
                                         from Account_List_Item_vod__r) 
                                 from Account_List_vod__c 
                                 where OwnerId in :MapOfTierObj.keyset()
                                 And Name =:ListName])
{
    if(!cpc.Account_List_Item_vod__r.isempty())
    {
        if(!MapOfAcctListItem.containsKey(cpc.OwnerId)){
            MapOfAcctListItem.put(cpc.OwnerId,cpc.Account_List_Item_vod__r);
        }
        else{    
            MapOfAcctListItem.get(cpc.OwnerId).addAll(cpc.Account_List_Item_vod__r);
        }
    }
}

I need to assign only Account_vod__c field values of in value section of MapOfAcctListItem Map instead of addAll(cpc.Account_List_Item_vod__r). How can i achieve that?

2

Thanks @Adrian I was able to compile my code with logic you suggested. But I need to check if data comes fine. Hopefully all do well here. Thanks!

Map<Id,set<Id>> MapOfAcctListItem = new Map<Id,Set<Id>>();

for(Account_List_vod__c cpc : [select Access_vod__c,Display_Order_vod__c,Icon_Name_vod__c,Id,Name,OwnerId, 
                                        (select ID,Account_vod__c,External_ID_vod__c 
                                         from Mapped_Accounts_vod__r) 
                                 from Account_List_vod__c 
                                 where OwnerId in :MapOfTierObj.keyset()
                                 And Name =:ListName]) {
    if(!cpc.Mapped_Accounts_vod__r.isempty())
    {   
        List<Account_List_Item_vod__c> results;
        results.addall(cpc.Mapped_Accounts_vod__r);
        Set<Id> resultIds =  new Set<Id>();

        for(Account_List_Item_vod__c sObj : results){
            resultIds.add(sObj.Account_vod__c);
        } 
        if(!MapOfAcctListItem.containsKey(cpc.OwnerId)){
            MapOfAcctListItem.put(cpc.OwnerId,resultIds);
        }
        else{    
            MapOfAcctListItem.get(cpc.OwnerId).addAll(resultIds);
        }
    }
}
  • Suggest you get rid of the results variable (that you are not initialising so NPE likely) and just loop directly over cpc.Mapped_Accounts_vod__r in the sObj loop. – Keith C Jan 19 '17 at 8:43

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.