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?

RCS
  • 2,110
  • 3
  • 14
  • 24
Testing_SFDC
  • 2,940
  • 10
  • 61
  • 142
  • 1
    Food for thought: http://salesforce.stackexchange.com/questions/8910/how-can-i-efficiently-generate-a-setid-from-a-listsobject-structure – Adrian Larson Jan 19 '17 at 07:04
  • Yes but this is applicable only when I need the Id field not for any other field. Here I need to assign Account_vod__c field so not same case here. – Testing_SFDC Jan 19 '17 at 07:09

1 Answers1

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);
        }
    }
}
RCS
  • 2,110
  • 3
  • 14
  • 24
Testing_SFDC
  • 2,940
  • 10
  • 61
  • 142
  • 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 08:43