-1

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 ????

3
  • Batch classes cannot execute by themselves. They always require some code in order to execute.
    – sfdcfox
    Commented Nov 7, 2021 at 15:14
  • you have some issues in the execute - t is not defined; con insert will fail without a LastName
    – cropredy
    Commented Nov 8, 2021 at 2:20
  • Hi Guys, Pls ignore that T and lastname I just want to know that can we write all logic in batch class only because i need to write one scheduler for batch class's to schedule it but as i know it wont work if I call batch class in schedule as it wont get the account id , Please help... Thanks :)
    – user105921
    Commented Nov 8, 2021 at 5:56

1 Answer 1

0

Short answer is no, you can definitely accomplish that without using batch class.

If you want more details you will have to be more specific:

  • How can we know what kind of Contact records we need to create?

You must log in to answer this question.

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