2
Account[] IPList = [SELECT Name,
                       (SELECT Product_Family__c 
                        FROM Product_Releases_del__r 
                        ORDER BY Product_Family__c ASC) 
                    FROM Account a 
                    WHERE ParentID =: AccountID];

I have the above query/subquery. It's a List of Lists that contains accounts and a list of account child objects. I need to essentially recreate the structure of the list of lists, so I can then exclude certain child objects based on their value. How would you go about iterating through both lists and adding the values to a list of Accounts/Child objects.

0

2 Answers 2

1

If the exclusions are simple you can do them at query time in the where:

IPList = [
        select Name, (
                select Product_Family__c
                from Product_Releases_del__r
                where ????
                ORDER BY Product_Family__c ASC
                )
        from Account
        where ParentID =: AccountID
        ];

Otherwise you will need to create your own class:

public AccountWrapper {
    public Account a {get; set;}
    public Product_Release_del__c[] prs {get; set;}
    public AccountWrapper(Account a) {
        this.a = a;
        this.prs = new Product_Release_del__c[] {}
    }
}

and do the filtering in Apex:

AccountWrapper[] ws = new AccountWrapper[] {};
for (Account a : [
        select Name, (
                select Product_Family__c
                from Product_Releases_del__r
                ORDER BY Product_Family__c ASC
                )
        from Account
        where ParentID =: AccountID
        ]) {
    AccountWrapper w = new AccountWrapper(a);
    for (Product_Release_del__c pr : a.Product_Releases_del__r) {
        if (pr ...) {
            w.prs.add(pr);
        }
    }
    ws.add(w);
}

because relationship collections can't be modified in Apex code once created via a query.

0

If you can't do the exclusions the way Keith C is suggesting, you can use a wrapper class to recreate the list, like this:

AccountWrapper[] wrapperList = new AccountWrapper[]{};
for (Account acct : IPList){
    AccountWrapper aw = new AccountWrapper();
    aw.acct = acct;
    if (!acct.Product_Release_del__c.isEmpty()){
        for (Product_Release_del__c : release : acct.Product_Release_del__c)
        aw.prodReleases.add(release);
    }
    wrapperList.add(aw);
}

public class AccountWrapper{
    Account acct {get;set;}
    Product_Releases_del__c[] prodReleases {get;set;}

    public AccountWrapper(){
        prodReleases = new Product_Releases_del__c[]{};
    }
}

Obviously you'll need to check the relationship names are correct, but this should do it.

You must log in to answer this question.

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