How to populate values into this map? There are three objects named job__c, employee__c and a junction object between job__c and employee__c named `JobEmployeeJunction__c´. I want the list of employees related to a job.

Map<Id,Map<Id,Employee__c>> mapOfIdToJunctionObjectToEmployee = new Map<Id,Map<Id,Employee__c>>();
For(JobEmployeeJunction__c jobEmpJunction : jobEmployeeJunctionList){
  For(Job__c jobVar : jobList){
      mapOfIdToJunctionObjectToEmployee.put(jobVar.id,jobEmpJunction.Employee__c, new Employee__c());
  • jobList = [Select Client__c,Client_Type__c,Amount_without_discount__c,Discount__c,Due_Amount__c,End_Date__c,Number_of_booking_days__c,Paid_Amount__c,Recurrences__c,Start_Date__c,Status__c,Type_of_job__c,Amount_to_be_paid__c from job__c where id IN :jobId]; jobEmployeeJunctionList = [Select Employee__c,Job__c,Amount_Paid_By_Client__c,Sallary_For_Employee__c,Start_Date__c,End_Date__c from JobEmployeeJunction__c where Job__c IN :jobId]; Jan 20, 2017 at 9:53

2 Answers 2


There are multiple ways to form the query/perform this task depending how you want to map to be.

According to your question you want all employees listed for each Jobs i.e Job Ids I assume. So below is a sample code which will form a Map of Job Ids to a list of employees who have applied for it.

The SOQL query which I used will fetch all the Employees in the system and the inner query will fetch all the associated Jobs to each employees. Next, I loop through each Employee's associated jobs and populate the Job Id to Employees map.

Map<Id, List<Employee__c>> mapOfJobIdToEmployees = new Map<Id, List<Employee__c>>();
for(employee__c emp : [select Id, (Select Id, job__c from JobEmployeeJunctions__r)  from employee__c])
    for(JobEmployeeJunction junctionRec : emp.JobEmployeeJunctions__r)
            mapOfJobIdToEmployees.put(junctionRec.job__c, new List<Employee__c>{emp});

Feel free to ask questions.


I don't think you need a complicated map of map in your scenario. I suggest to you to use the Query child-to-parent relationships in soql (link to SFDC docs) to query the junction object and get all the information you need from the related object (Job__c / Employee__c )

    List<JobEmployeeJunction__c> jobEmployeeJunctionList = [Select Employee__c,Job__c,Amount_Paid_By_Client__c,Sallary_For_Empl‌​oyee__c,Start_Date__‌​c,End_Date__c,Job__r.Client__c,Job__r.Client_Type__c,Job__r.Amount_without_discount__c,Job__r.Discount‌​__c,Job__r.Due_Amount__c,Job__r.En‌​d_Date__c,Job__r.Number_of_‌​booking_days__c,Job__r.Paid‌​_Amount__c,Job__r.Recurrenc‌​es__c,Job__r.Start_Date__c,Job__r.‌​Status__c,Job__r.Type_of_jo‌​b__c,Job__r.Amount_to_be_pa‌​id__c from JobEmployeeJunction__c where Job__c IN :jobId]; 

    Map<Id,List<Id>> mapOfIdJobToEmployeeIdList = Map<Id,List<Id>>();
    for(JobEmployeeJunction__c jobEmpJunction : jobEmployeeJunctionList){
        List<Id> employeeIdList;
            //There is no key: Instantiate a new list
            employeeIdList = new List<Id>();
            //There is a Key in the map: a list is already instantiated
            employeeIdList = mapOfIdJobToEmployeeList.get(jobEmpJunction.Job__c);

        //add the Id of the employee
        mapOfIdJobToEmployeeList.put(jobEmpJunction.Job__c, employeeIdList);

You must log in to answer this question.

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