+ Start a Discussion
Hari N 20Hari N 20 

Generate records from one object to another using batch apex

Hi All,
I want to generate records from one object into another object. I have written a trigger for this and working fine.
I want this output in batch apex form
What ever I get the records in object1 today, I want a schedule batch apex that all records should get create in object2 at Midnight only.
How can I do this?
Please provide me syntax

Thanks in Advance

Regards,
Hari
Dilip_VDilip_V
Hi Hari,

Batch Class:
global class ProcessRecords implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,Name FROM Object1 where DAY_ONLY(CreatedDate) = :Date.today()';
        return Database.getQueryLocator(query);
    }
   
    global void execute(Database.BatchableContext BC, List<Account> scope) {
     List<Object2> NewObjs=new List<Object2>();
         for(Object1 Obj1 : scope)
         {
             Object2 Obj2=new Object2();
             Obj2.Name = Obj2.Name;      
             NewObjs.add(Obj2);      
         }
         Insert NewObjs;
    }   
    
    global void finish(Database.BatchableContext BC) {
    }
}

Shedulable class:
 
Global class ScheduleProcessRecords  implements Schedulable{

    global void execute(SchedulableContext SC) 
    {
        ProcessRecords MUB = new ProcessRecords ();
        Database.executeBatch(MUB);
    }
}
Cron expression;
 
ScheduleProcessRecords  machinestatus = new ScheduleProcessRecords ();
String sch = '0 0 23 * * ?';
system.schedule('Process Records', sch, machinestatus);

Let me know if you have any issue.

Mark it as best answer if it works.

THanks.
Hari N 20Hari N 20
Hi Thermo,
I have implemeted the same.
But, I am not able to see in apex jobs. Even record also not created
Dilip_VDilip_V
Hari,

Have you executed cron expression in execute anonymous?If don't plese do that.
The code will run everyday at 11:00 PM.So you won't see results right away 

Thanks.
Hari N 20Hari N 20
Sure.. I will do that
Thank you
Hari N 20Hari N 20
Hi Thermo,
It is not working. I have changed scheduling time to 6 PM.
I checked after 6 PM. No record is created in Object2.
Please help me. 
Mahesh K 22Mahesh K 22
Hi Hari ,
Batch Apex Class:
---------------------------------------------------------------
global class BatchapexExmple implements database.Batchable<Sobject> {

    global database.QueryLocator start(database.BatchableContext bc){
        string query = 'select id,name,phone,CreatedDate from account where CreatedDate =Today';
        return database.getQueryLocator(query);
    }
    global void execute(database.BatchableContext bc , list<account> acc ){
        list<contact> con = new list<contact>();
        for(account a : acc){
            contact c = new contact();
            c.LastName = a.name;
            c.Phone = a.phone;
            con.add(c);
        }
        insert con;
    }
    global void finish(database.BatchableContext bc){
        
    }
}

Schedulable apex Class:
-------------------------------------------------------------------------
global class ScheduableApex implements Schedulable{
    global void execute(schedulableContext sc){
        BatchapexExmple ba = new BatchapexExmple();
        database.executeBatch(ba);
    }
}

Execution:
---------------------------------------------------
ScheduableApex sc2 = new ScheduableApex();
string str = '59 59 23  * * ?';
system.schedule('job2', str, sc2);