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);
           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 Successfully' message in the debug log. However, the records are not moving from Course_temp__c to Course__c. What am I missing here?


1 Answer 1


Thanks for all your suggestions guys, the code is working now. I commented out the constructor and delete scope from the code I posted above, and it's somehow working. I know these might not be the real reason why the code wasn't working before, but I don't know what else is different now. Thanks again, I really appreciate it.

  • Can you please mark this answer as accepted so the question no longer shows as open?
    – Adrian Larson
    Apr 24, 2019 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.