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
lakshya agrawallakshya agrawal 

create a batch class to insert custom object records from each opportunity in the system

Suraj Tripathi 47Suraj Tripathi 47
Hi lakshay,
Batch class:-
public class InsertObj implements
    Database.Batchable<sObject>, Database.Stateful {
  
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT ID,name,stagename from opportunity LIMIT 100'
        );
    }
    public void execute(Database.BatchableContext bc, List<opportunity> scope){
        List<CustomObj__c> objList= new List<CustomObj__c>();
        for (opportunity opp: scope) {
          CustomObj__c obj =  new CustomObj__c();
          obj.name=opp.name;
          obj.oppId__c=opp.id;
          objList.add(obj);
        }
       if(objList.size()>0){
           insert objList;
       }
    }
    public void finish(Database.BatchableContext bc){
       
    }
}
 
Database.executeBatch(new InsertObj, 20);

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
lakshya agrawallakshya agrawal
my custom object is piet so how i write
 create a batch class to insert PIET records from each opportunity in the system??
 
lakshya agrawallakshya agrawal
obj.oppId__c=opp.id;
error in this line
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Lakshya,

You can create the lookup field (field name->oppId ) relationship with opportunity inside the piet object .

Thanks