I have requirement When account is created then contact should get created and linked to that particular account. Can we write batch class for it, i have written a Batch with Trigger
trigger ChildCreation on Account(after insert, before Delete) {
if (Trigger.isAfter && Trigger.isInsert){
ESRT_Batch_Tour_Email_Creation b = new ESRT_Batch_Tour_Email_Creation(Trigger.new);
Database.executeBatch(b);
}
================================
Batch Class
global class ESRT_Batch_Tour_Email_Creation implements Database.Batchable <sObject> {
List<Account> acc;
public ESRT_Batch_Tour_Email_Creation (List<Account> acc{
this.acc = acc;
}
global Database.QueryLocator start (Database.BatchableContext bc){
String query = 'SELECT Id,Name,Tour_Date__c,Tenant__r.Name,Broker__r.Name,(Select id,Property__r.Name From Contact) from Account Where ID IN: acc';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<Account> batch) {
List<Contact> c = new List<Contact>();
for(Account a : batch){
if(t.Tour_Date__c < date.today()){
Contact con = new Contact();
con.Account = a.Id;
c.add(con);
}
}
}
insert c;
}
global void finish(Database.BatchableContext bc) {
//Do Nothing.
}
}
Can we write all logic in batch class only ????
t
is not defined;con
insert will fail without aLastName