0

I have to make all accounts into different groups based on the primary_Number__c field value and then have to assign these values to a map method. I have written the following code where I am able to pring the values of my primarynumber field but dont understand how can I make a group. Note: I have two fields on account called Group_ID__cand Primary_Number__c where GroupID is different for every account and the PrimaryNumber is not different for every account.

public static Map<Account, List<Account>> groupAccounts(List<Account> accounts){
Map<Account, List<Account>>  primaryNumbers = new Map<Account, List<Account>>();
Map<String, String> primaryNumberGroups = new Map<String, String>();
//groupby primary numbers
//todo....
List<Id> primaryNumberIds =new List<Id>();
for(Account acc: aacounts){
primaryNumberGroups.put(acc.Group_Id__c, acc.Primary_Number__c);}
promoteBusinesstoPrimaryNumbers(primaryNumbers);
return null;
}

PS: promoteBusinesstoPrimaryNumbers is an another method which I have under this method where I am checking the group is having a business account or not. "groupAccounts" method I am using in my trigger handler class to get the groups of all accounts. Here I just want to get all the groups on the account object. Plaese help me in getting it.

Thanks in advance

1 Answer 1

0

This code is perhaps what you are looking for. Each map value is the one or more Account records that have the same Group_ID__c:

public static Map<Account, List<Account>> groupAccounts(List<Account> accounts) {
    Map<Account, List<Account>> m = new Map<Account, List<Account>>();
    for (Account a : accounts) {
        String k = a.Group_ID__c;
        List<Account> v = m.get(key);
        if (v == null) {
            v = new List<Account>();
            m.put(k, v);
        }
        v.add(a);
    }
    return m;
}

You must log in to answer this question.

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