1

I have been working on a customized apex:repeat PDF list and the requirements have changed. I can't seem to wrap my head around customizing apex:repeat in the way I would like to. I am close, but am missing two parts.

  1. I need to move the highlighted user's name to the top of the list. Is there a way in apex to customize a list to say something like if(User.Leadership__c != null) { put this user at the top of their list};
  2. If there are more then one highlighted managers in the Sales Department list, do not include the list.

VFP:

<apex:page renderAs="pdf" showHeader="false" applyBodyTag="false" controller="phoneListController" >
<div class="container">
    <h2>Title</h2>
    <p>Employee Phone and Extension List</p>
    <div>
        <table>
            <tbody>
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Phone</th>
                    <th>Ext.</th>
                    <th>Title</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 style="{!IF(person.Leadership__c == true, 'background-color: #efefef', '')}">{!person.Name}</td>
                        <td style="{!IF(person.Leadership__c == true, 'background-color: #efefef', '')}">{!person.Phone}</td>
                        <td style="{!IF(person.Leadership__c == true, 'background-color: #efefef', '')}">{!person.Extension}</td>
                        <td style="{!IF(person.Leadership__c == true, 'background-color: #efefef', '')}">{!person.Title}</td>
                    </tr>
                </apex:repeat>
            </apex:repeat>
            </tbody>
        </table>
    </div>
</div>
</apex:page>

Controller:

public with sharing class phoneListController {
    public Datetime changeToPst { get; set; }
    public String newDate { get; set; }
    public List<User> hilightName {get;set;}

    public Map<String, User[]> allPeople {
        get {
            Map<String, User[]> teams = new Map<String, User[]>();

            List<User> users = [SELECT Name, Title, Department, Extension, Phone, Manager_for_Reports__c, Manager.Name, UserRole.Name, Leadership__c FROM User WHERE UserType != 'Partner' AND UserType != 'Power Partner' AND IsActive = true AND Title != null ORDER BY Department ASC, Name ASC];

            List<User> leaderShip = [SELECT Name, Title, Department, Extension, Phone, Manager_for_Reports__c, Manager.Name, UserRole.Name, Leadership__c FROM User WHERE UserType != 'Partner' AND UserType != 'Power Partner' AND Leadership__c = TRUE AND IsActive = true AND Title != null ORDER BY Department ASC, Name ASC];
            Map<String, User> mapLeaderToDepartment = new Map<String, User>();

            List<User> departList = [SELECT Name, Title, Department, Extension, Phone, Manager_for_Reports__c, Manager.Name, UserRole.Name, Leadership__c FROM User WHERE UserType != 'Partner' AND UserType != 'Power Partner' AND Leadership__c = TRUE AND IsActive = true AND Title != null ORDER BY Department ASC, Name ASC];
            Map<String, User> mapTheDepartment = new Map<String, User>();

        for (User l : leaderShip) {
            mapLeaderToDepartment.put(l.Name, l); //string key, user value
        }

        for (User d : departList) {
            mapTheDepartment.put(d.Department, d); //string key, user value
        }

        for (User u : users) {
            if (u.Department != null) {
                if (u.Department == 'Sales') {
                   User manager = mapLeaderToDepartment.get(u.Manager.Name);
                       if (u.Manager.Name != null) {
                           String teamName = u.Department + ' - ' + u.Manager.Name;
                           //has a manager and has a team already listed
                           //it does not understand u.UserRole.Name --if(u.UserRole.Name != 'Director of Sales') {
                              if (teams.containsKey(teamName)) {
                                 teams.get(teamName).add(u);
                                 system.debug('got into 1');
                              }
                              //has a manager but does not have a team listed yet
                              else {
                                 teams.put(teamName, new User[]{
                                    manager, u
                                    });
                                 system.debug('got into 2');
                              }
                            //}
                        }
                        else {
                            //this section handles the two sales managers
                            String teamName2 = u.Department;
                            //has a manager and has a team already listed
                            if (teams.containsKey(teamName2)) {
                                teams.get(teamName2).add(u);
                                system.debug('got into 3');
                            }
                            //has a manager but does not have a team listed yet
                            else {
                                teams.put(teamName2, new User[]{
                                        manager, u
                                });
                                system.debug('got into 4');
                            }
                        }
                    }

                //everyone else
               else {
                   if (teams.containsKey(u.Department)) {
                       teams.get(u.Department).add(u);
                       system.debug('got into 5');
                       //if the team has already been created, then the Leadership__c person should already be included
                   }
                    else {
                      teams.put(u.Department, new User[]{u});
                      //teams.put(u.Department, new User[]{manager, u}); -- this will add the manager name to the top of the list but I need the person in the list with the Leadership__c checkbox = true
                      system.debug('got into 6');
                    }
                }
            }
        }
        return teams;
        }
        set;
    }
}

So what I have tried:

-> Mapping the User with the Leadership__c checkbox checked to their department name

-->Looping through User list and removing the user with Leadership__c checked, and then adding it back in

--> Conditionals on UserRole

--> Looking up ways to remove, contains, clone, maps inside of maps, formatting lists into strings, I have no luck

I apologize for the length of this question. I am super stuck!

Picture of what it looks like:

what is currently looks like

Picture what what I want it to look like:

what I want it to look like

As you can see from the pictures, the person highlighted should be on the top of the list. The highlighted person has the field Leadership__c field checked.

Also, Sales Team Drew K. is not in the new list. He is the top guy meaning all of the managers report to him, which is why his whole list is highlighted.

The requirement is that I do not hard code anything. So I can not write a conditional that says "exclude the list belonging to Drew K." The list has to be completely dynamic.

0

I feel a code of below type can help where you are rearranging user lists before sending it to user interface:

//traverse on final map prepared by getter just before return statement
for(String key:teams.keyset()){
    List<User> userValues=teams.get(key);
    userValues=rearrangeUsers(userValues);
    if(userValues!=null)
       teams.put(key,userValues);
    else
       teams.remove(key);
    }

//This method will put leaders on top of list. will return null if all are leaders.
public List<User> rearrangeUsers (List<User> usersToArrange){
    List<User> leaderUsers=new List<User>();
    List<User> nonLeaderUsers=new List<User>();
    for(User u:usersToArrange){
        if(u.Leadership__c!=null)
            leaderUsers.add(u);
        else
            nonLeaderUsers.add(u);
        }
    if(nonLeaderUsers.size()==0)
        return null;
    else
        leaderUsers.addAll(nonLeaderUsers);
    return leaderUsers;
    }
| improve this answer | |
  • good suggestion, I am working through this. It does not like the param that is being passed into rearrangeUsers(). I am getting error Error:(122, 32) expecting a semi-colon, found '(' and when I edit it by putting it inside of another method, removing the ( and add a semi colon, I get the same error. When I take out the param it obviously does not understand what rearrangeUsers(userValues) is – Olivia Jan 25 '17 at 18:25
  • What about arranging in apex class itself. I mean do not bind rearrangeusers method on Visualforce. – Mukesh Verma Jan 25 '17 at 18:29
  • I coorected a mistake. It should be public List<User> rearrangeUsers (List<User> usersToArrange){ – Mukesh Verma Jan 25 '17 at 18:33
  • it needs a value to be passed into the rearrangeUsers(userValues); When I pass in the list of users, such as departList, it is able to save but I get back no data. The list is empty. – Olivia Jan 25 '17 at 18:53
  • is that issue has been solved? – Santanu Boral Jan 25 '17 at 23:12
0

If I get this requirement, I will think of like this way.

Approach

Rather than creating 3 queries for user, department and leadership, I will create single query which will give a me ordered list of Department, then Leadership (checked will be sorted first), then Name

Since List is ordered so manipulation is easy and your requirement is the Department Leader's Name will be displayed first, and then their subordinates.

I have taken your example and tried to portray as below when the SOQL query will run, it will give this kind of results.

Person Hierarchy

//list will contain all users ordered by Department, LeaderShip(True), User Name
List<User> allUserList = [SELECT Name, Title, Department, Extension, Phone, Manager_for_Reports__c, Manager.Name, UserRole.Name, Leadership__c 
FROM User 
WHERE UserType != 'Partner' AND UserType != 'Power Partner'
AND IsActive = true AND Title != null 
ORDER BY Department ASC, Leadership__c  ASC, Name ASC];

for(User userObj:allUserList){
    if (userObj.Department != null) {
        if(userObj.Department == 'Sales') {
            //if user is Manager then add him to the list
            if(userObj.Leadership__c){
                String teamName = userObj.Department + ' - ' + userObj.Manager.Name;
                teams.put(teamName, new List<User>().add(userObj) );
            }
            else {
                //for normal user, search for his Manager and put him into that list
                if(teams.containsKey(userObj.Department + ' - ' + userObj.Manager.Name))
                {
                    List<User> userList = teams.get(userObj.Department + ' - ' + userObj.Manager.Name);
                    teams.put(userObj.Department + ' - ' + userObj.Manager.Name, userList.add(userObj));
                }                           
            }       

        }else
        {
            //check department is already exists in the list
            if(teams.containsKey(userObj.Department)) {
                //get the existing list based on key
                List<User> userList = teams.get(userObj.Department);

                //add the user into that department
                teams.put(userObj.Department,userList.add(userObj));
            }
            else {
                //department doesn't exist in the list
                teams.put(userObj.Department,new List<User>().add(userObj));
            }
        }
    }
}

Taking this approach, I think you will stay away of deleting the user from the map which you were doing now.

Note: I haven't compiled the code, just tried to give you the simplified approach. Feel free the change according to your requirement.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.