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){
/*Execute*/
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,
RecordType.DeveloperName
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
);
lstContact.add(cont);
}
Insert lstContact;
}
}
global void finish(Database.BatchableContext BC)
{
}