I have a bach Apex process Which will Insert in 3 object with related fields.

Account, Contact, and Program (Custom Object)

Acccount is related to Muliple Contact and a Contact is related to Multiple Program (Custom Object).

Now, I am trying to insert custom object(Program) which has lookup to contact as follows:

for(emitcs1__Students_Programs_Temp__c tempProg: program){
                Program__c prg = new Program__c (
                    prg.emitcs1__Program__c =prg.emitcs1__Program__c
                    prg.emitcs1__Student__c = // I  want to get the Contact Record Id( Related Id)


Is there a way to achieve inserting all 3 in single DML call with related fields. Thanks

private Id enrollmentId;
    public string enrollmentStatus ='Submitte for Review';
    public MoveEnrollmentToTransaction(Id strParam) {
        enrollmentId = strParam;

    global Database.QueryLocator start(Database.BatchableContext bc){       
        system.debug('enrollmentId' + enrollmentId);
        string query= 'SELECT Id, emitcs1__Enrollment__c, Name, emitcs1__Phone__c, emitcs1__Service_Center__c, emitcs1__Street__c, emitcs1__State__c, emitcs1__City__c, emitcs1__Postal_Code__c, emitcs1__Country__c FROM emitcs1__Family__c WHERE emitcs1__Enrollment__c=:enrollmentId and emitcs1__Enrollment_Status__c=: enrollmentStatus';        
        return Database.getQueryLocator(query);

    global void execute(Database.BatchableContext BC,  List<sObject> scope){

        List<emitcs1__Family_Members__c> familymember =[Select Id, emitcs1__FirstName__c, emitcs1__LastName__c, emitcs1__Phone__c, emitcs1__Email__c,
                                                        emitcs1__Student_Relation__c, emitcs1__Communicate_with_Contact__c, emitcs1__Grade__c, emitcs1__Gender__c,
                                                        emitcs1__Birthdate__c, emitcs1__School__c, emitcs1__Allergy_Information__c, emitcs1__Special_Request__c,
                                                        from emitcs1__Family_Members__c];

         List<emitcs1__Students_Programs_Temp__c> program =  [Select Id FROM emitcs1__Students_Programs_Temp__c where emitcs1__Student__c in:familymember];

        for(emitcs1__Family__c tempFamily :(List<emitcs1__Family__c>) scope){
            system.debug('tempFamily' + tempFamily.Name);

            Account acct = new Account (   
                Name = tempFamily.Name,
                Family_Name__c =  tempFamily.Name,
                Home_Phone__c = tempFamily.emitcs1__Phone__c,
                Enrollment_Name__c = tempFamily.Id,
                Enrollment__c =  tempFamily.Name,
                //UserName__c = tempFamily.Family_Email__c,
                ParentId = tempFamily.emitcs1__Service_Center__c,
                Enroll_Status__c = 'Enroll In Progress',
                RecordTypeId = '0122w000000HLpnAAG'
            Insert acct;

            List<Contact> lstContact = new list<Contact>();
            for(emitcs1__Family_Members__c tempFamMem: familymember){
                 Contact cont = new Contact (
                     FirstName= tempFamMem.emitcs1__FirstName__c,
                     LastName= tempFamMem.emitcs1__LastName__c,
                     Phone= tempFamMem.emitcs1__Phone__c,
                     Email= tempFamMem.emitcs1__Email__c,
                     AccountId = acct.Id
            Insert lstContact;


    global void finish(Database.BatchableContext BC)

1 Answer 1


The process for inserting multiple sObject types in a single DML operation is well documented in Creating Parent and Child Records in a Single Statement Using Foreign Keys in the Apex Developer Guide.

The short summary (from this document) is that

You can use external ID fields as foreign keys to create parent and child records of different sObject types in a single step instead of creating the parent record first, querying its ID, and then creating the child record...

If you don't have External Id fields already defined on the objects you're using, it's generally not worth it to change your data model and define such fields just to save one DML operation. Properly-bulkified code rarely gets into trouble using one DML statement for each sObject type being modified.

If the problem to be solved is that it's challenging to properly maintain complex relationship networks while inserting bulkified data, consider using some permutation of the Unit of Work pattern in a library (such as fflib, demonstrated in the linked Trailhead module) that provides out of the box support for such operations.

You must log in to answer this question.

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