You need to sign in to do that
Don't have an account?
RarLopz
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?
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
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
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 ?
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
Thanks!!
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?