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
RarLopzRarLopz 

Iterating through Map List

I am trying to iterate over a list in the map, get distinct phone numbers from Accounts in the list and assign the phone number/s in a long text area on new line or comma separated. My attempt so far. .. 
I would be grateful for any suggestions? 
 
//1. Fetch all Accounts associated with each CompanyID__c
//2.  if the List of Accounts for every Key has more than one record then
//3.  fetch the distict phone numbers on all of those accounts and // struggling
//4. store them in a Long Text Area field on new line   // struggling


List<Account> accs = [SELECT Id, Name, CompanyID__c, Phone FROM Account where CompanyID__c!=null];
Map<String, List<Account>> mapR1Accs = new Map<String, List<Account>>();
for (Account a : accs) {
    if (mapR1Accs.containsKey(a.CompanyID__c)){
        mapR1Accs.get(a.CompanyID__c).add(a);
    } else{
        List <Account> lstaccts = new List <Account> ();
    	lstaccts.add(a);
        mapR1Accs.put(a.CompanyID__c,lstaccts);
    }
}

system.debug('This is the map: ' +mapR1Accs);

//For Each of the Key 
 for(String accR1ID : mapR1Accs.keySet()){
    List<Account> accounts = mapR1Accs.get(accR1ID);
    System.debug('There are ' + accounts.size() + ' accounts for companyid ' + accR1ID);
    if (accounts.size()>1) 
		for (Account myaccount : accounts) {
			
			System.debug('The phone number for ' + myaccount.id + ' is ' +myaccount.Phone);
			// How do I get the phone numbers from each of the account in the list ? and 
			// how to I assign it to Backup_PhoneNumber__c long text area field, each phone number on a new line?
			// How do I update these accounts ? 
			// myaccount.Backup_PhoneNumber__c = myaccount.Phone; 
		}
	}
}


This is my debug log so far...


16:23:53:046 USER_DEBUG [13]|DEBUG|This is the map: {IGOAL=(Account:{Id=001M00000170ooaIAA, Name=Cruise - R1 INTERNAL, CompanyID__c=IGOAL, Phone=123-456-7890, RecordTypeId=012E0000000RTEdIAO}, 
															Account:{Id=001M00000170oozIAA, Name=Cruise WEST, CompanyID__c=IGOAL, Phone=222-333-4444, RecordTypeId=012E0000000RTEdIAO}), 
													 ZY9JW=(Account:{Id=001M00000170opsIAA, Name=Ferrari, CompanyID__c=ZY9JW, Phone=333-444-5555, RecordTypeId=012E0000000RTEdIAO},
															Account:{Id=001M00000170optIAA, Name=Malibu, CompanyID__c=ZY9JW, Phone=333-444-5555, RecordTypeId=012E0000000RTEdIAO})}


16:23:53:046 USER_DEBUG [18]|DEBUG|There are 2 accounts for companyid IGOAL
16:23:53:047 USER_DEBUG [22]|DEBUG|The phone number for 001M00000170ooaIAA is 123-456-7890
16:23:53:047 USER_DEBUG [22]|DEBUG|The phone number for 001M00000170oozIAA is 222-333-4444

16:23:53:047 USER_DEBUG [18]|DEBUG|There are 2 accounts for companyid ZY9JW
16:23:53:047 USER_DEBUG [22]|DEBUG|The phone number for 001M00000170opsIAA is 333-444-5555
16:23:53:047 USER_DEBUG [22]|DEBUG|The phone number for 001M00000170optIAA is 333-444-5555

 
Raj VakatiRaj Vakati
Use this code
 
//1. Fetch all Accounts associated with each CompanyID__c
//2.  if the List of Accounts for every Key has more than one record then
//3.  fetch the distict phone numbers on all of those accounts and // struggling
//4. store them in a Long Text Area field on new line   // struggling


List<Account> accs = [SELECT Id, Name, CompanyID__c, Phone FROM Account where CompanyID__c!=null];
Map<String, List<Account>> mapR1Accs = new Map<String, List<Account>>();
for (Account a : accs) {
    if (mapR1Accs.containsKey(a.CompanyID__c)){
        mapR1Accs.get(a.CompanyID__c).add(a);
    } else{
        List <Account> lstaccts = new List <Account> ();
    	lstaccts.add(a);
        mapR1Accs.put(a.CompanyID__c,lstaccts);
    }
}

system.debug('This is the map: ' +mapR1Accs);
Set<String> uniquePhones = new Set<String>();
//For Each of the Key 
 for(String accR1ID : mapR1Accs.keySet()){
    List<Account> accounts = mapR1Accs.get(accR1ID);
    System.debug('There are ' + accounts.size() + ' accounts for companyid ' + accR1ID);
    if (accounts.size()>1) 
		for (Account myaccount : accounts) {
			System.debug('The phone number for ' + myaccount.id + ' is ' +myaccount.Phone);
			uniquePhones.add(myaccount.Phone);
		}
	}

	String mySet_Joined_Exception = String.join(uniquePhones, ', ');

	
	
	}

 
RarLopzRarLopz
@RajVakati

String mySet_Joined_Exception = String.join(uniquePhones, ', ');   // error : Method does not exist or incorrect signature: [Set<String>]

I tried following and it worked --- 
String mySet_Joined_Exception = String.join((Iterable<String>)UniquePhoneNumbers, '\n');    // i changed to '\n' for new line

 
RarLopzRarLopz
I encountered one issue as i tested ...

myaccount.Data_Loss_Phone_Test__c = String.join((Iterable<String>)UniquePhoneNumbers, '\n ');  

if i assign this ouside the outer FOR loop I get an error, variable does'nt exist myaccount

how can i assign the string in the Set to each Account associated with only the accounts assciated with current accR1ID ?


 
//For Each of the Key 
 for(String accR1ID : mapR1Accs.keySet()){
    	List<Account> accounts = mapR1Accs.get(accR1ID);
        if(accounts.size()>1){
            System.debug('There are ' + accounts.size() + ' accounts for companyid ' + accR1ID);
            for (Account myaccount : accounts) {
                System.debug('The phone number for ' + myaccount.id + 'is ' +myaccount.Phone);
                UniquePhoneNumbers.add(myaccount.Phone);
        		
            }
        }
     	////myaccount.Data_Loss_Phone_Test__c = String.join((Iterable<String>)UniquePhoneNumbers, '\n '); 
 }



 
Raj VakatiRaj Vakati
Give me complete code
RarLopzRarLopz

This is the output i am looking after updating the Accounts associated with the key: companyid

companyid IGOAL
so, after updating I should see the following for Account Id: 001M00000170ooaIAA   Backup_PhoneNumber__c = 123-456-7890,222-333-4444
                                                  Account Id: 001M00000170oozIAA   Backup_PhoneNumber__c = 123-456-7890,222-333-4444
                                                  
                                                  

companyid GHDSA                                                 
so, after updating I should see the following for Account Id: 001M00000170oobGFAA   Backup_PhoneNumber__c = 888-999-7890,111-111-1212
                                                  Account Id: 001M00000170ookLNBA   Backup_PhoneNumber__c = 888-999-7890,111-111-1212

 
List<Account> accs = [SELECT Id, Name, CompanyID__c, Phone FROM Account where CompanyID__c!=null];
Map<String, List<Account>> mapR1Accs = new Map<String, List<Account>>();
for (Account a : accs) {
    if (mapR1Accs.containsKey(a.CompanyID__c)){
        mapR1Accs.get(a.CompanyID__c).add(a);
    } else{
        List <Account> lstaccts = new List <Account> ();
    	lstaccts.add(a);
        mapR1Accs.put(a.CompanyID__c,lstaccts);
    }
}

system.debug('This is the map: ' +mapR1Accs);
set<String> UniquePhoneNumbers = new set<String>();
//For Each of the Key 
 for(String accR1ID : mapR1Accs.keySet()){
    	List<Account> accounts = mapR1Accs.get(accR1ID);
        if(accounts.size()>1){
            System.debug('There are ' + accounts.size() + ' accounts for companyid ' + accR1ID);
            for (Account myaccount : accounts) {
                System.debug('The phone number for ' + myaccount.id + 'is ' +myaccount.Phone);
                UniquePhoneNumbers.add(myaccount.Phone);
        		//myaccount.Data_Loss_Phone_Test__c = String.join((Iterable<String>)UniquePhoneNumbers, '\n ');
            }
        }
     	 myaccount.Data_Loss_Phone_Test__c = String.join((Iterable<String>)UniquePhoneNumbers, '\n ');
 			
 		
 }
update accs;

Thanks!! 
Raj VakatiRaj Vakati
try this
List<Account> accs = [SELECT Id, Name, CompanyID__c, Phone FROM Account where CompanyID__c!=null];
Map<String, List<Account>> mapR1Accs = new Map<String, List<Account>>();
for (Account a : accs) {
    if (mapR1Accs.containsKey(a.CompanyID__c)){
        mapR1Accs.get(a.CompanyID__c).add(a);
    } else{
        List <Account> lstaccts = new List <Account> ();
    	lstaccts.add(a);
        mapR1Accs.put(a.CompanyID__c,lstaccts);
    }
}

system.debug('This is the map: ' +mapR1Accs);
set<String> UniquePhoneNumbers = new set<String>();
//For Each of the Key 
 for(String accR1ID : mapR1Accs.keySet()){
    	List<Account> accounts = mapR1Accs.get(accR1ID);
        if(accounts.size()>1){
            System.debug('There are ' + accounts.size() + ' accounts for companyid ' + accR1ID);
            for (Account myaccount : accounts) {
                System.debug('The phone number for ' + myaccount.id + 'is ' +myaccount.Phone);
                UniquePhoneNumbers.add(myaccount.Phone);
        		//myaccount.Data_Loss_Phone_Test__c = String.join((Iterable<String>)UniquePhoneNumbers, '\n ');
            }
        }
		
 			
 		
 }
 for (Account myaccount : accs) {
     	 myaccount.Data_Loss_Phone_Test__c = String.join((Iterable<String>)UniquePhoneNumbers, '\n ');
		}
		
update accs;

 
RarLopzRarLopz
That didn't work .   It populated every record with all phone numbers in the set.  So all the four of my test records has this value in the DataLossPhoneTest field.

User-added image
RarLopzRarLopz
I am wondering if i could  create a Map <Id, set<String>>
where  ID will be the account  Id's associted with the companyid and set<string> = UniquePhoneNumbers

and then for every pass of  for(String accR1ID : mapR1Accs.keySet()){     

assign the string in Set to the Data_Loss_Phone_Test__c  in list of accounts)  . update the Id in the Map

Not sure how to do it though. Any sugestions?