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
Jyothi Priya JarangJyothi Priya Jarang 

i want to create a batch class to fetch data from staging object and insert into 2 different objects. ?This is urgent.Please help

AnudeepAnudeep (Salesforce Developers) 
Hi Jyothi, 

Posting here a sample batch class that you can use as a reference
 
global class batchTwo Implements Schedulable, Database.Batchable<sObject>{
global void execute(SchedulableContext sc) {
    Database.executeBatch(this);
}

global database.queryLocator start(Database.BatchableContext BC) {
    
    
        return database.getQueryLocator([SELECT Id,AccountId__c,F1__c,F2__c FROM Obj1__c]);
    
} 

global void execute(Database.BatchableContext BC, list <obj1__c> scope) {

    List <Task> taskList = new List<Task>();

   Map<Id, Obj2__c> mapObj2=new Map<Id, Obj2__c>();
   
   for(Obj2__c obj2 : [SELECT Account_Name__r.Id,Obj2_F2__c FROM Obj2__c])
{
        myMap.put(obj2.Id, obj2);
}
    
    for(Obj1__c c : scope) {
            Task tsk             = new Task();
            if(mapObj2.containsKey(c.AccountId__c)) {
             Obj2__c tmpSales = mapObj2.get(c.AccountId__c);
            
             tsk.F1__c=tmpSales.Obj2_F2__c;
             
            }
            
            tsk.WhatId           = c.AccountId__c;
            tsk.ActivityDate     = System.today();
            tsk.Status           = 'Open';
            tsk.Priority         = 'Normal';
            tsk.Subject          = 'call';
            tsk.P1__c=c.F1__c;
            tsk.P2__c=c.F2__c;
            
           taskList.add(tsk);
    } //close for-loop

   
        insert taskList;
    
       
    
} 

global void finish(Database.BatchableContext BC) {


} 
}

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Jyothi Priya JarangJyothi Priya Jarang
Here is my code:
i have created Emp__c as master object, Trainings__c as Child of Emp__c and Staging__C as the staging object.
while loading data into staging , i checked for duplcaites and if there is no duplicate, i want to create Emp__c record and Trainings__c record (data is available in Staging)
I can insert into master object but not able to insert into child.

global class batch implements Database.Batchable<sObject> {
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String query = 'SELECT id,Name,Emp_Name__c,Training_Name__c    FROM Staging__C ';
        
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Staging__C> scope)
    {
                     
                Map<string,Staging__C> newAccRecMap = new Map<string,Staging__C>();
                for(Staging__C stg : scope)
                    {
                    newAccRecMap.put(stg.Emp_Name__c,stg);   
                        }
       
                        List<Staging__C> List2=[select ID,Emp_Name__c from Staging__C where Emp_Name__c IN: newAccRecMap.keySet() Limit 50000];
                       
                        if(List2.size() > 0)
                         {
                            for(Staging__C acc : List2)
                            {
                                if(newAccRecMap.containsKey(acc.Emp_Name__c))
                                {
                                   // acc.Emp_Name__c=acc.Emp_Name__c+'staging';
        System.debug('hehe3');
                                    System.debug(acc);
                                    System.debug(List2);
                                }
                                
                             }   update List2;
                            System.debug('hehe4');
                         }
                       
                            System.debug('hehe1');
                            List<Emp__c> emp=new List<Emp__c>();
                            for(Staging__C stg1: scope)
                            {
                                System.debug(scope);
                                Emp__c emp1=new Emp__c();
                                
                                emp1.Name = stg1.Emp_Name__c;
                                //emp1.Email__c='abc@gmail.com';
                               // emp1.Phone__c= 0000;
                                emp.add(emp1);
                                System.debug('emp');
                            }
                            insert emp;
                           List<Training__c> Childs = new List<Training__c>();
                     
                                for(Staging__C stg2 : scope)
                                {
                                    System.debug(stg2);
                                   Training__c Child = new Training__c ();
                                   Child.Employee__c = stg2.Emp_Name__c;
                                   Child.Name = stg2.Training_Name__c; 
                                   Childs.add(Child);      
                                }
                            
                                insert Childs;
                        
      
    }
    
    global void finish(Database.BatchableContext BC)
    {
        System.debug('>>>Finish');
    }
}
Jyothi Priya JarangJyothi Priya Jarang
From staging..i m trying to insert into child:
for(Staging__C stg2 : scope)
                                {
                                    System.debug(stg2);
                                   Training__c Child = new Training__c ();
                                   Child.Employee__c = stg2.Emp_Name__c;
                                   Child.Name = stg2.Training_Name__c; 
                                   Childs.add(Child);      
                                }
                            
                                insert Childs;

How to link to parent??