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 PIET (custom object) records from each opportunity in the system

CharuDuttCharuDutt
Hii Lakshya Agarwal
Try Below 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'
        );
    }
    public void execute(Database.BatchableContext bc, List<opportunity> scope){
        List<PIET__c> lstP= new List<PIET__c>();
        for (opportunity opp: scope) {
          PIET__c p =  new PIET__c();
          p.name = opp.name;
          p.oppId__c=opp.Id;
          lstP.add(p);
        }
       if(lstP.size()>0){
           insert lstP;
       }
    }
    public void finish(Database.BatchableContext bc){
       
    }
}


Test Class


@isTest
public class InsertObjTest {
@isTest
    Public Static Void UnitTest(){
        list<Opportunity> lstopp = new list<opportunity>();
        for(integer i=0;i<5;i++){
            opportunity opp = new opportunity();
            opp.Name = 'test opp '+ i;
            opp.CloseDate = system.today();
            opp.StageName = 'Closed Won';
            lstOpp.add(opp);
        }
        insert lstOpp;
    
    InsertObj lp = new InsertObj();
        database.executeBatch(lp);
    }
}
Please Mark It As Best Answer If It Helps
Thank You!

 
lakshya agrawallakshya agrawal
errors are coming
 
CharuDuttCharuDutt
What Errors are Showing
lakshya agrawallakshya agrawal
in this:-

p.oppId__c=opp.Id; 

and there is need to write a test class 
and how to execute this batch class??
 
CharuDuttCharuDutt
Hii  Lakshya

This line is For p.oppId__c=opp.Id;  if Your Custom Object Have Lookup RelationShip With Opportunity.

If Your Custom object have opportunity lookup Field Then Change This p.oppId__c To p.opprtunitylookupFieldname In Your Org.
Suraj Tripathi 47Suraj Tripathi 47
Hi lakshya,

Greetings!

Here is your batch class code, try this.
    
public class InsertCustomObjectReocrds implements
    Database.Batchable<sObject>, Database.Stateful {
  
    public Database.QueryLocator start(Database.BatchableContext bc) {
        return Database.getQueryLocator(
            'SELECT Id, Name, Stagename, CloseDate from Opportunity'
        );
    }
    public void execute(Database.BatchableContext bc, List<opportunity> oppList){
        List<PIET__c> listPIET = new List<PIET__c>();
        for (Opportunity opp: oppList) {
          PIET__c pObj =  new PIET__c();
          pObj.Name = opp.Name;
          pObj.Opportunity__c = opp.Id;
          listPIET.add(pObj);
        }
       if(!listPIET.isEmpty()){
           insert listPIET;
    system.debug('PIETList ---->' + listPIET);
       }
    }
    public void finish(Database.BatchableContext bc){
       AsyncApexJob job = [SELECT Id, Status,NumberOfErrors, JobItemsProcessed,TotalJobItems FROM AsyncApexJob WHERE Id=:bc.getJobId()];
       system.debug('job==>'+job);
    }
}


---------------------------------------------------------------------------
If this helped you, please mark it as the best answer. 

Thank you!

Regards,
Suraj Tripathi
 
CharuDuttCharuDutt
Hii lakshya
Try Below Code
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'
        );
    }
    public void execute(Database.BatchableContext bc, List<opportunity> scope){
        List<PIET__c> lstP= new List<PIET__c>();
        for (opportunity opp: scope) {
          PIET__c p =  new PIET__c();
          p.name = opp.name;
          p.Opportunity__c=opp.Id;
          lstP.add(p);
        }
       if(lstP.size()>0){
           insert lstP;
       }
    }
    public void finish(Database.BatchableContext bc){
       
    }
}
Please Mark It As Best Answer If It Helps
Thank You!