I have a phone list of users and have each department titled. If it's a sales department, the sales manager name is listed next to the department name (see picture).
I want the sales manager to show up under their own department. The logic I have now is grouping people under their sales managers. So if Bob Smith is a manager but Bob Smith's manager is John Doe, then Bob Smith will end up under John Doe's list. I need Bob Smith in his own list as well.
I have tried a number of variations of adding the managers in the list for a second time, but I get stuck because when I do this it adds the manager to everyone's list. I can't use JavaScript conditionals because it will be a PDF.
I need some logic that states: add the department manager's name back into their own list. Any advice on how to write the conditional? I am unsure how to add the name back in only by department.
What the list looks like now:
Controller:
public Map<String, User[]> allPeople {
get {
Map<String, User[]> tmp = new Map<String, User[]>();
//looping through list
for (User u : [SELECT Name, Title, Department, Extension, Phone, Manager_for_Reports__c FROM User WHERE UserType != 'Partner' AND UserType != 'Power Partner' AND IsActive = true AND Title != null ORDER BY Department ASC, Name ASC]) {
//checking only in sales departments
if (u.Department == 'Sales' && u.Manager_for_Reports__c != null) {
//getting all of the Manager names in the SOQL query and adding the string 'Sales Team' to the front of it
//saving this data into array uList
User[] uList = tmp.get('Sales Team ' + u.Manager_for_Reports__c);
system.debug('this is the uList:' + uList);
//checking to see if uList is empty
if (uList == null) {
//if it is empty- create a new user array
uList = new User[]{};
//add to the uList array all of the data in the u SOQL query
uList.add(u);
//creating another list of managers and adding it back into the uList
for (User nm : [SELECT Name, Title, Department, Extension, Phone, Manager_for_Reports__c FROM User WHERE UserType != 'Partner' AND UserType != 'Power Partner'AND IsActive = true AND Title != null AND Sales_Leadership__c = true AND Department = 'Sales' ORDER BY Name ASC]){
//adding the sales manager back into their own list only
*****if(tmp.get(u.Manager_For_Reports__c) == u.Name)*****{
//I can't just add nm into uList again, if I do this it will add the names to every department. I need to only add it to specific departments
uList.add(nm);
system.debug('this is the value of the uList:' + uList);
}
}
tmp.put('Sales Team ' + u.Manager_for_Reports__c, uList);
system.debug('this is the tmp:' + tmp);
}
else {
uList.add(u);
system.debug('this is the value of u: ' + u);
tmp.put('Sales Team ' + u.Manager_for_Reports__c, uList);
}
}
else {
User[] uList = tmp.get(u.Department);
if (uList == null) {
uList = New User[]{};
uList.add(u);
tmp.put(u.Department, uList);
}
else {
uList.add(u);
tmp.put(u.Department, uList);
}
}
}
return tmp;
} set;}
Visualforce:
<table>
<tbody>
<thead>
<tr>
<th>Name</th>
<th>Title</th>
<th>Ext.</th>
<th>Phone</th>
</tr>
</thead>
<apex:repeat value="{!allPeople}" var="depts">
<th colspan="5" id="department">{!depts}</th>
<apex:repeat value="{!allPeople[depts]}" var="person">
<tr>
<td>{!person.Name}</td>
<td>{!person.Title}</td>
<td>{!person.Extension}</td>
<td>{!person.Phone}</td>
</tr>
</apex:repeat>
</apex:repeat>
</tbody>
</table>
UPDATE:
This is what I want the list to look like:
UPDATE2: Is there a way to do a .contains() in the key of a map? I was thinking something like this might work as well...
for (User nm : [SELECT Name, Title, Department, Extension, Phone, Manager_for_Reports__c FROM User WHERE UserType != 'Partner' AND UserType != 'Power Partner'AND IsActive = true AND Title != null AND Sales_Leadership__c = true AND Department = 'Sales' ORDER BY Name ASC]){
if(tmp.get('Sales Team ' + u.Manager_for_Reports__c).contains(nm.name)){
uList.add(nm);
}