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
TK1234TK1234 

Batch Apex to insert new records

Hi Experts,

I am trying to do bulk insert of new accounts in SF using batch apex concept.
 I went thru the Database.Batchable<sObject>  and guess the records fetched in Database.QueryLocator is what processed in execute method .... which means it does only update/delete? how to insert record?

how i can accomblish inserting new records in bulk >50k in SF. Kindly assist.

Also to start with i  tried with update... I dont see any records updated.... instead i see in console with message ("source for Batch Apex could not be loaded) here is the sample.... please let me know where am missing.

global class batchExample implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute         
        String query = 'SELECT Id,Name FROM Account limit 10'  ;         
        return Database.getQueryLocator(query);       
    }
     
    global void execute(Database.BatchableContext BC, List<Account> scope) {
        List<Account> accList = new List<Account>();
        // process each batch of records
          for (integer i=0;i<100;i++)
                {
        Account anew= new Account(Name ='theanu'+i,BillingCity='SA port'+i,Phone='1212'+i);
        accList.add(anew);         
            
               }
         
        try {
            // Update the Account Record
            insert accList;
            
         
         
        } catch(Exception e) {
            System.debug(e);
        }
         
    }        
    global void finish(Database.BatchableContext BC) {
        // execute any post-processing operations
       
  }
}



 

v varaprasadv varaprasad
Hi,

I have executed your code on the console it is working fine and it inserts 100 records.
If you want to insert more than 100 than change   for (integer i=0;i<100000;i++)
                {​
 
batchExample bc = new batchExample();
Database.executeBatch(bc);




Hope this helps you!

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com

 
TK1234TK1234
oh is it? for me it keeps throwing message as "source for batch apex could not be loaded " :(

I want to insert some 100k records...I tried same execution .. didnt work :(
Sree07Sree07
Did u find any solution for this?