function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ramin MohammadiRamin Mohammadi 

removing child object from account list

Guys, 

I have a query/subquery that gives me this.
IPList = [select Name,(select Account__c, Product_Family__c from Product_Releases_del__r ORDER BY Product_Family__c ASC) from Account a where ParentID =: AccountID];

The issue is, I need the same list to only show unique "Product Family" fields. The way its set up now, an account can have multiple unique Product releases that have duplicate product family fields. 

Two things came to mind:

- Alter the query to only get one object per Product Family (which I didn't know how to do)
- Loop through the list and delete duplicate Product Family objects.

The code:

    FTemp = '';
    
    For(Account acc: IPList){
        integer i = 0;
        For(Installed_Products__c p: acc.Product_Releases_del__r){
            i = i + 1;
            if( p.Product_Family__c == FTemp){
                acc.remove(p[i]);
            }
            else {
                FTemp = p.Product_Family__c;
            }                       
        }

     }

The FTemp variable serves as a check for duplicates as the queried list is sorted alphabetically. Right now I am getting "Expression must be a list type: Installed_Products__c" error.

The main point is that I need IPlist to only contain installed products with unique "Product Family" Fields. 
Any ideas would be appreciated, the correct suggestion recieves kudos
Pankaj MehraPankaj Mehra
Hi Ramin, 

You can use Map to store the details, the data structure would be something like this

Map<Id,Map<String,Product_Releases_del__r >> 

Here outer most map will contain Account id and inner map will contain exactly one record for unique Product_Family__c

This is how we must fill the map :

Map<Id,Map<String,Product_Releases_del__c >>  accountProductMap = new Map<Id,Map<String,Product_Releases_del__c >> ();

// this is bulkified version , you will have only single account in query
for (Account a : [select Name,(select Account__c, Product_Family__c from Product_Releases_del__r ORDER BY Product_Family__c ASC) from Account a where ParentID =: AccountID]) {
// loop for childs
for(Product_Releases_del__c pr : a.Product_Releases_del__r ) {
// If account is not present  
if(!accountProductMap.containsKey(a.Id)) {
    accountProductMap.put(a.Id,new Map<String,Product_Releases_del__r >());
}

// fill only unique Product_Family__c 

Map<String,Product_Releases_del__r > productMap = accountProductMap.get(a.Id);
productMap.put(pr.Product_Family__c ,pr);
accountProductMap.put(a.Id , productMap);
}

}

//////////////////////////////////////////////////////////////////////////////////////

This will definitly solve your problem , please let me know in case you need any help :)

 
Ramin MohammadiRamin Mohammadi
It works, but this is part of a controller extension for a visualforce page. The outer map needs to reference Account so that it can be called in a standard VF controller