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
Galeeb SKGaleeb SK 

insert records through batch apex

Hai, i tried to insert records through batch apex but it can't ,tell me ,my code is

global class opportunityincrement implements database.batchable<sObject>{
 global database.QueryLocator start(database.batchableContext bc){
  string query='select id,Name,Job_Description__c,Max_Pay__c,Min_Pay__cfrom Position__c';
  return database.getQueryLocator(query);
  }
 global void execute(database.batchableContext bc,list<Position__c>scope){
  system.debug(scope+'bbbbbbbbbbbbbbbbbbb');
 
  list<Position__c> oppty=new list<Position__c>();
   for(Position__c opp:scope){
  
  
   opp.Name='Test';
   opp.Job_Description__c= 'Closed';
   opp.Max_Pay__c= 10000;
    opp.Min_Pay__c= 5000;
   
    oppty.add(opp);
     system.debug(oppty+'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa');
  
   
   }
    insert oppty;
   }
   global void finish(database.batchableContext bc){
   
   }
  }
Best Answer chosen by Galeeb SK
RAM AnisettiRAM Anisetti
Hi ,

modify your code like this...
 
global class opportunityincrement implements database.batchable<sObject>
{
  global database.QueryLocator start(database.batchableContext bc){
  string query='select id,Name,Job_Description__c,Max_Pay__c,Min_Pay__c from Position__c';
  return database.getQueryLocator(query);
  }
  global void execute(database.batchableContext bc,list<Position__c>scope){
  system.debug(scope+'bbbbbbbbbbbbbbbbbbb');
 
  list<Position__c> oppty=new list<Position__c>();
   for(Position__c opp:scope){
  
 //dont use opp here...

  Position__c c=new Position__c();
   c.Name='Test';
   c.Job_Description__c= 'Closed';
   c.Max_Pay__c= 10000;
   c.Min_Pay__c= 5000;
   
   oppty.add(c);
  
  
   
   }

   
    insert oppty;
   }
   global void finish(database.batchableContext bc){
   
   }
  }

 

All Answers

Abhishek BansalAbhishek Bansal
What is the error you are facing
RAM AnisettiRAM Anisetti
Hi ,

modify your code like this...
 
global class opportunityincrement implements database.batchable<sObject>
{
  global database.QueryLocator start(database.batchableContext bc){
  string query='select id,Name,Job_Description__c,Max_Pay__c,Min_Pay__c from Position__c';
  return database.getQueryLocator(query);
  }
  global void execute(database.batchableContext bc,list<Position__c>scope){
  system.debug(scope+'bbbbbbbbbbbbbbbbbbb');
 
  list<Position__c> oppty=new list<Position__c>();
   for(Position__c opp:scope){
  
 //dont use opp here...

  Position__c c=new Position__c();
   c.Name='Test';
   c.Job_Description__c= 'Closed';
   c.Max_Pay__c= 10000;
   c.Min_Pay__c= 5000;
   
   oppty.add(c);
  
  
   
   }

   
    insert oppty;
   }
   global void finish(database.batchableContext bc){
   
   }
  }

 
This was selected as the best answer
Abhishek BansalAbhishek Bansal
Yes, I think Ram is right.
But there are few things in Ram's code which can alos be modified.
Please update code as follows :

global class opportunityincrement implements database.batchable<sObject> {
    global database.QueryLocator start(database.batchableContext bc){
        string query='select id,Name,Job_Description__c,Max_Pay__c,Min_Pay__c from Position__c';
        return database.getQueryLocator(query);
    }
    global void execute(database.batchableContext bc,list<Position__c> scope){
        system.debug(scope+'bbbbbbbbbbbbbbbbbbb');
 
        List<Position__c> oppty = new List<Position__c>();
        for(Integer i=0 : i < scope.size() ; i++){
            //dont use opp here...

            Position__c c=new Position__c();
            c.Name='Test';
            c.Job_Description__c= 'Closed';
            c.Max_Pay__c= 10000;
            c.Min_Pay__c= 5000;
            oppty.add(c);
        }
        insert oppty;
   }
   global void finish(database.batchableContext bc){
   
   }
}



Let me know if you need more help on this.

Regards,
Abhishek
Sree07Sree07
I have a reuirement like I have to insert 60000 records and later update the rating field to warm for all the account records.How can i insert 60000 records using batch apex?It showing an error like:First error: Too many DML rows: 10001


global class BatchAccounts implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT Id,Name FROM Account'  ;         
        return Database.getQueryLocator(query);       
    }
    
    global void execute(Database.BatchableContext BC, List<Account> scope) {
        List<Account> accList = new List<Account>();
        for (integer i=1;i<6;i++)
        {
            Account anew= new Account(Name ='Account '+i);
            anew.Type = 'Prospect';
            accList.add(anew); 
        }
            upsert accList; 
    }
    global void finish(Database.BatchableContext BC) {
    }
}


Can anyone help me with this?