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
sooraj kesavadassooraj kesavadas 

Apex batch Job

I am trying to move records from one custom Object(Course_temp__c) to another custom Object(Course__c) using Apex batch. This is the code I have:
global class MyBatchJob2 implements Database.Batchable<Course_temp__c> {

    global MyBatchJob2(){}

    global List<Course_temp__c> start(Database.BatchableContext BC) {
    	return [Select Id, Name, Contact__c, Course_Fees__c,Date__c From Course_temp__c];
    }

    global void execute(Database.BatchableContext BC, List<Course_temp__c> scope) {
       List<Course__c> lhList = new List<Course__c>();
       for(Course_temp__c obj : scope){
           System.debug('Course_temp records are: ' +obj);
           lhList.add(
               new Course__c(
                  Name = obj.Name,
                   Contact__c = obj.Contact__c,
                   Course_Fees__c = obj.Course_Fees__c,
                   Date__c = obj.Date__c
               )
           );
           System.debug('The list is: '+lhlist);
       }
       insert lhList;
       delete scope;
    }

    global void finish(Database.BatchableContext BC) {
        System.debug('Finished Succesfully');
    }
}
I am invoking the batch class using           Id batchJobId = Database.executeBatch(new MyBatchJob2(), 200);

The code is running without any compilation errors and I also get the 'Finished Succesfully' message in the debug log. However, the records are not moving from Course_temp__c to Course__c. Can someone please tell me what I missing. Any help is hugely appreciated.


 
Best Answer chosen by sooraj kesavadas
Nish321Nish321
global class MyBatchJob2 implements Database.Batchable<Course_temp__c> {

//    global MyBatchJob2(){}     remove this

    global List<Course_temp__c> start(Database.BatchableContext BC) {
    	return [Select Id, Name, Contact__c, Course_Fees__c,Date__c From Course_temp__c];
    }

    global void execute(Database.BatchableContext BC, List<Course_temp__c> scope) {
       List<Course__c> lhList = new List<Course__c>();
       for(Course_temp__c obj : scope){
           System.debug('Course_temp records are: ' +obj);
           lhList.add(
               new Course__c(
                  Name = obj.Name,
                   Contact__c = obj.Contact__c,
                   Course_Fees__c = obj.Course_Fees__c,
                   Date__c = obj.Date__c
               )
           );
           System.debug('The list is: '+lhlist);
       }
       insert lhList;
    //   delete scope;   This is not required
    }

    global void finish(Database.BatchableContext BC) {
        System.debug('Finished Succesfully');
    }
}