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
Indhu priya 17Indhu priya 17 

Create Group of accounts based on a custom field value and assign it to a map method

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__c and 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
List<Id> primaryNumberIds =new List<Id>();
for(Account acc: aacounts){
primaryNumberGroups.put(acc.Group_Id__c, acc.Primary_Number__c);}
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
Julio Alberto Asenjo GarcíaJulio Alberto Asenjo García
Hi, I hope this help you. This method group a list of accounts based on a custom field. But i don't understand, what promoteBusinesstoPrimaryNumbers  do.
public static Map<Account, List<Account>> groupAccounts(List<Account> accounts){

    Map<String, List<Account>>  mapAccountsByGroup = new Map<String, List<Account>>();//Map to group accounts by group
    Map<String, List<String>>  mapPrimaryNumberByGroup = new Map<String, List<String>>(); // Map to group primary numbers by group. If you want unique values use a set insted of a list

    for(Account acc: aacounts){
        //If the map contains the Group just add new element to the list
            mapPrimaryNumberByGroup.get(acc.Group_Id__c).add(acc.Primary_Number__c );
            // If not you should put a new element
            List<Account> lAcc= new List<Account>();
            List<String> lString new List<String>();
return mapAccountsByGroup;