0

How would you insert multiple records into the junction object?

I have the following Junction object name (Associate_Employee__c) with the following fields:

Employee_Request__c
Employer_Request__c 

both are master_detail relationship

List<Associate_Employee__c> aeList = new List<Associate_Employee__c>();
List<Employee_Request__c> empList = new List<Employee_Request__c>();
List<Employer_Request__c> emplorList = new List<Employer_Request__c>();

Associate_Employee__c pa = new Associate_Employee__c(); 
Employee_Request__c empReq = new Employee_Request__c();
//record 1 
empReq.Project_Name = 'SFDC1'; 
empList.add(empReq);
//record 2
empReq = new Employee_Request__c();
empReq.Project_Name = 'SFDC2'; 
empList.add(empReq);
insert empReq; //INSERT

aeList.Employee_Request__c = empReq.id; // ??? How can I reference? If you got multiple records?

same goes with the other object Employer_Request__c

1
  • if you have multiple master records then on what basis you will enter records in junction object? – Santanu Boral Jun 23 '17 at 22:26
1

Here is an small attempt to retrieved multiple records from insertion and create Junction object records.

Leverage Database.insert() method which will return SaveResult and has getId() method to retrieve Id which has been inserted.

This kind of logic might help you!

List<Associate_Employee__c> aeList = new List<Associate_Employee__c>();
List<Employee_Request__c> empList = new List<Employee_Request__c>();
List<Employer_Request__c> emplorList = new List<Employer_Request__c>();


//insert Employee request record
for(integer i=0; i<5; i++)
{
    Employee_Request__c empReq = new Employee_Request__c();
    empReq.Project_Name = 'SFDCempReq' + i; 
    empList.add(empReq);
}
Database.SaveResult[] srEmpList = Database.insert(empList, false);

//insert Employer request record
for(integer i=0; i<5; i++)
{
    Employer_Request__c employerReq = new Employer_Request__c();
    employerReq.Project_Name = 'SFDC2' + i; 
    emplorList.add(employerReq);
}

Database.SaveResult[] srEmployerList = Database.insert(emplorList, false);

//loop through the SaveResults to get the inserted Ids in the loop and prepare junction records.
for (Database.SaveResult srEmp : srEmpList) {
    if (srEmp.isSuccess()) 
    {

        for(Database.SaveResult srEmployer : srEmployerList)
        {
            if(srEmployer.isSuccess())
            {
                Associate_Employee__c assoEmp = new Associate_Employee__c();
                assoEmp.Employee_Request__c =  srEmp.getId();
                assoEmp.Employer_Request__c = srEmployer.getId();
                aeList.add(assoEmp);
            }           
        }
    }    
}
insert aeList;
1

You just add each one separately. If you had one Employer_Request__c Id to join to many Employee_Request__c Ids, it would look something like:

List<Associate_Employee__c> associateEmployees = new List<Associate_Employee__c>();
// ...
Employee_Request__c employeeRequest = new Employee_Request(/*data*/);
List<Id> employerRequestIds = ...;
for (Id employerRequestId : employerRequestIds)
{
    associateEmployees.add(new Associate_Employee__c(
        Employee_Request__c = employeeRequest.Id,
        Employer_Request__c = employerRequestId
    ));
}

Note that naming a variable empReq or empList is very ambiguous here.

3
  • in the above sample code how do think the best way to get the Ids? List<Id> employerRequestIds – Nick Kahn Jun 23 '17 at 21:37
  • I don't know...you're the one writing the code...you didn't provide that context. – Adrian Larson Jun 23 '17 at 21:38
  • correct, if you have a single record you get the Id after you insert the Employee_Request but in my question I have comment how would you do if you have multiple records inserting? – Nick Kahn Jun 23 '17 at 21:40

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.