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
durgaprasad vdpdurgaprasad vdp 

how to transfer records one batch class to another batch class

how to transfer records from one batch class to another batch class
NagendraNagendra (Salesforce Developers) 
Hi Durga,

Please check with below example which copies data from login history object to custom login history object.

You have to create a batch job with a scope of records from LoginHistory in the start method of your batch job.  The trick here potentially is checking for duplicate records, e.g. LoginHistory records that may or may not already exist in the LoginHistory custom object.  

Next, your execute method would create a new list of custom LoginHistory records and then insert them.  Note that the max batch size is 200 records, so that means your batch job will run in "chunks" of 200 records at a time, but for simply copying from one object to another, you should have no problem with that.

To run the batch job on a regular basis, you'll want to call it from a Schedulable class.  See here for more info on that:
http:// http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm (http:// http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm)
global class CopyLoginHistory implements Database.Batchable<LoginHistory> {

    global CopyLoginHistory(){}

    global List<LoginHistory> start(Database.BatchableContext BC) {
    	return [Select <fields> From LoginHistory Where //condition to prevent dupe records.. may be by time frame?];
    }

    global void execute(Database.BatchableContext BC, List<LoginHistory> scope) {
       List<LoginHistory__c> lhList = new List<LoginHistory__c>();
       for(LoginHistory lh : scope){
           lhList.add(
               new LoginHistory__c(
                  field1__c=lh.Field1
               )
           );
       }
       insert lhList;
    }

    global void finish(Database.BatchableContext BC) {}
}
For more information check with below link.
  • http://salesforce.stackexchange.com/questions/66754/apex-batch-to-transfer-and-delete-data
Please mark this as solved if the information helps.

Best Regards,
Nagendra.
 
durgaprasad vdpdurgaprasad vdp
Hi Nagendra here what  i am asking I have two batch classes like batchClassA and bactchClassB so i want to transfer records batchClassA to batchClassB
mukesh guptamukesh gupta
Hi Durga,

Can you please explain your requirment. why you want to do this. 

Regards
Mukesh
durgaprasad vdpdurgaprasad vdp
Actually this is not my requirement scenario  i got just idea can we do this or not like that,  we can  call one batch to another batch correct so i am trying to transfer records one batch class to another batch class 
Ajay K DubediAjay K Dubedi
Hi  Durgaprasad,
You should follow this code for transfer records one batch class to another batch class
First batch class:
global class RecalculateTaskRollupBatch implements Database.Batchable<sObject>, Database.Stateful
{
    global final String query;
    global final Integer batchSize;
    global Set<Id> parentIds = new Set<Id>();
    global Set<Id> tasksToUpdate = new Set<Id>();
    global RecalculateTaskRollupBatch(Integer updateBatchSize)
    {
        batchSize = updateBatchSize;
        query = 'SELECT Id, AccountId, WhoId FROM Task'; //ALL TASKS!
    }
    global Database.QueryLocator start(Database.BatchableContext BC)
    {  
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Task> scope)
    {  
        // Only add one task per parent to tasksToUpdate
        for(Task t : scope)
        {
            if(t.AccountId != NULL && !parentIds.contains(t.AccountId))
            {
                tasksToUpdate.add(t.id);
                parentIds.add(t.AccountId);
            }
            if(t.WhoId != NULL && !parentIds.contains(t.WhoId))
            {
                if(!tasksToUpdate.contains(t.id)) tasksToUpdate.add(t.id);
                parentIds.add(t.WhoId);
            }
        }
    }
    global void finish(Database.BatchableContext BC)
    {
        if(tasksToUpdate.size() > 0)
        {
            Database.executeBatch(new UpdateTasksBatch(tasksToUpdate),batchSize);
        }
    }
}

Second batch class:
global class UpdateTasksBatch implements Database.Batchable<sObject> {
    String query;
    Set<Id> tasksToUpdate;
    global UpdateTasksBatch(Set<Id> tasksPassed) {
        tasksToUpdate = tasksPassed;
        query = 'SELECT Id FROM Task WHERE Id IN :tasksToUpdate';
    }
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Task> scope) {
        update scope;
    }
    global void finish(Database.BatchableContext BC) {
        /*
        Eventually, I would love to keep track of failed batches and retry them
        */
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi