function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC GuestSFDC Guest 

Batch apex class to create multiple child records for a Parent record based on Date

Hi All,

I have created batch apex class to create multiple customers if account has no customers. Below is the code which creates one customer after running batch apex. 
But my requirement is to create 7 customers from Date given in Account record. 
Example: If Date__c in Account is 25-10-2017, then 7 customers. 1st customer should be created with Date__c = 25-10-2017, 2nd customer should be created with Date__c = 26-10-2017 till 7 days.


Below batch class creates a single customer for Account which has no customer:

global class batchContactCreate implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
       return Database.getQueryLocator([
                select Name, Date__c from Account where ID NOT IN (SELECT Account__c from S1_Customer__c)]);  
    }
    
   
    global void execute(Database.BatchableContext BC, List<Account> scope) {
         List<S1_Customer__c> cList = new List<S1_Customer__c>();
         for(Account a : scope)
         {
            S1_Customer__c cust = new S1_Customer__c(Name =a.Name, Account__c = a.Id, Phone__c = '1212121232');
            cList.add(cust); 
                   
         }
         insert cList;
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}

Thanks in advance
Best Answer chosen by SFDC Guest
Sergio AVSergio AV
I guess you just need something like..
 
global void execute(Database.BatchableContext BC, List<Account> scope) {
    List<S1_Customer__c> cList = new List<S1_Customer__c>();
    for(Account a : scope)
    {
        for(Integer i = 0; i < 7; i++)
        {
            S1_Customer__c cust = new S1_Customer__c(
                Name = a.Name, 
                Account__c = a.Id, 
                Phone__c = '1212121232', 
                Date__c = a.Date__c.addDays(i)
            );
            cList.add(cust);
        }      
    }
    insert cList;
}

And remember, if it solves your question, I'd appreciate if you mark this a the solution ;)

All Answers

Sergio AVSergio AV
I guess you just need something like..
 
global void execute(Database.BatchableContext BC, List<Account> scope) {
    List<S1_Customer__c> cList = new List<S1_Customer__c>();
    for(Account a : scope)
    {
        for(Integer i = 0; i < 7; i++)
        {
            S1_Customer__c cust = new S1_Customer__c(
                Name = a.Name, 
                Account__c = a.Id, 
                Phone__c = '1212121232', 
                Date__c = a.Date__c.addDays(i)
            );
            cList.add(cust);
        }      
    }
    insert cList;
}

And remember, if it solves your question, I'd appreciate if you mark this a the solution ;)
This was selected as the best answer
Shivdeep KumarShivdeep Kumar
Hi,

As per my understanding, you want to change the created date but you can not do this.

Thanks
Shivdeep

 
SFDC GuestSFDC Guest
Hi Sergio AV,

Thanks. That's working.
 Can you please let me know how to create child record Invoice__c with field values Name, Customer__c for each S1_Customer__c.

Suppose, if there are 7 customers, for every customer, it should have child record Invoice record.