1

I am trying to sort out the best direction to go in. I have a phone list with a department header and the employees under each department. I have done this with a map. My next task is to take the Sales department and split up all of the employees under each of their managers. Would a map inside of the map be a good solution or would a wrapper class be the best direction?

With the wrapper class, I am not merging two different objects rather I am merging different fields under one Object (department field, name field, manager field). So I feel like this is not the best direction to go in.

The requirements: no javascript can be used(it will be a pdf), all department names are at the top of a list of employees, sales manager names are apart of the department title in the Sales Department.

Suggestions?

public Map<String,User[]> allPeople{
    get{
        Map<String,User[]> tmp = new Map<String,User[]>();
        Map<String,User[]> salesMgmt = new Map<String,User[]>();
        //List<User> salesManager = [SELECT Name, Manager_for_Reports__c, Department FROM User WHERE Department = 'Sales' ORDER BY Manager_for_Reports__c, Name ASC];
        for(User u : [SELECT Name,Title, Department, Extension, Phone, Manager_for_Reports__c FROM User ORDER BY Department ASC, Name ASC]){
            //we take the user department and save it in array list uList
            User[] uList = tmp.get(u.Department);
            //if uList is null, then instantiae a new user for uList
            if(uList == null) uList = New User[]{};
            //add to uList the entire query results from line 68
            uList.add(u);
            //tmp is a Map, we are putting into the map the department and the data from uList
            tmp.put(u.Department,uList);
        }
        //we return the map
        return tmp;
    }
    set;
}

What I want it to look like:

What I want it to look like

What it does look like:

what it does look like

UPDATE

This does not work because the method is incorrect: [String].add(String) but maybe?

if(u.Department == 'Sales' && u.Manager_for_Reports__c != null){
    //we will change the department name and save it back into the list
    u.Department.add('Sales Team ' + u.Manager_for_Reports__c);
}

1 Answer 1

1

You can take a try like this without using Wrapper class:

Approach is, if Sales Manager exists then user will be added under that Manager. For all other cases it will add under department.

public Map<String,User[]> allPeople{
    get{
        Map<String,User[]> tmp = new Map<String,User[]>();
        //Map<String,User[]> salesMgmt = new Map<String,User[]>();
        //List<User> salesManager = [SELECT Name, Manager_for_Reports__c, Department FROM User WHERE Department = 'Sales' ORDER BY Manager_for_Reports__c, Name ASC];
        for(User u : [SELECT Name,Title, Department, Extension, Phone, Manager_for_Reports__c FROM User ORDER BY Department ASC, Name ASC]){
            //we take the user department and save it in array list uList

            if(u.Department == 'Sales' && u.Manager_for_Reports__c != null){
                User[] uList = tmp.get('Sales Team ' + u.Manager_for_Reports__c);
                if(uList.size()==0) uList = New User[]{};
                  uList.add(u);
                tmp.put('Sales Team ' + u.Manager_for_Reports__c, uList);
            }
            else
            {
                User[] uList = tmp.get(u.Department);
                //if uList is null, then instantiae a new user for uList
                if(uList == null) uList = New User[]{};
                //add to uList the entire query results from line 68
                uList.add(u);
                //tmp is a Map, we are putting into the map the department and the data from uList
                tmp.put(u.Department,uList);
            }
        }
        //we return the map
        return tmp;
    }
    set;
}
1
  • great suggestion. I had to edit a few lines to mesh with the code but it worked! Thank you!
    – Olivia
    Commented Dec 21, 2016 at 22:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .