I have written a batch class where when parent is created then child get created and relate to it. But when I manually create the parent record, child record get create in bulk.
I want when one parent is created then related to it one child should get created.
Below is the code:
Batch
global class ESRT_Batch_Tour_Email_Creation implements Database.Batchable <sObject> {
List<Leasing_Tour_Email_Queue__c> Lea = new List<Leasing_Tour_Email_Queue__c>();
global Database.QueryLocator start (Database.BatchableContext bc){
String query = 'SELECT Id,Name,Tour_Date__c from Tour__c';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<Tour__c> batch) {
for(Tour__c tou : batch){
Leasing_Tour_Email_Queue__c t = new Leasing_Tour_Email_Queue__c();
t.Tour__c = tou.Id;
Lea.add(t);
}
insert lea;
}
global void finish(Database.BatchableContext bc) {
//Do Nothing.
}
}
Trigger
trigger LeasingTourBatc on Tour__c (after insert) {
if (Trigger.isAfter && Trigger.isInsert){
for(Tour__c To : Trigger.new){
ESRT_Batch_Tour_Email_Creation b = new ESRT_Batch_Tour_Email_Creation();
Database.executeBatch(b);
}
}
}