+ Start a Discussion
Iqra TechIqra Tech 

how can i impliment this senario

global class MyBatchJob2 implements Database.Batchable<Account_Product__c>,Database.stateful {

global MyBatchJob2(){}

global List<Account_Product__c> start(Database.BatchableContext BC) {
    return [Select id,name,Account__c,AccountProductCatgory__c,Product_Category__c,Products__c, Show__c,Account__r.id 
           From Account_Product__c where  Account__r.id !=null];
}

global void execute(Database.BatchableContext BC, List<Account_Product__c> scope) {
   List<Account_Product_Batch__c> lh = new List<Account_Product_Batch__c>();
   for(Account_Product__c obj : scope){
       System.debug('Course_temp records are: ' +obj);
       lh.add(
           new Account_Product_Batch__c(
           name=obj.name,
              Account_Name__c=obj.Account__c, 
                       Account_Product_Category__c=obj .AccountProductCatgory__c,
                       Product_Category__c=obj.Product_Category__c,
                        Products__c=obj.Products__c,
                        Show__c=obj.Show__c
                        //acc_id__c=obj.Account__r.id 
                    
       ));
       System.debug('The list is: '+lh);
   }
   insert lh;
   //delete scope;
}

i created one batch class for copying all the data from "Account Product" object to my newly created custom object "Account Product Batch". the batch copied all the records sucessfully.
Now i need to copy all the Account's related contact too in "Account Product Batch" from "Account Product".
Tarun Khatri (TK)Tarun Khatri (TK)
Hello,

You can create another batch job similar to above one for Contacts. You start method will be like below:
 List<Id> accIdList = new List<Id>();
  for(Account_Product__c accProd  : [Select Id, Account__c FROM Accout_Product__c WHERE  Account__c != null]){
accIdList.add(accProd.Account__c);
}
   return[Select FirstName, LastName, Phone, Email FROM Contact WHERE AccoutId IN: accIdList ] ;
Now your scope variable will contain all contact records. Use it in execute method to insert records in Account Product Batch.


-Thanks,
TK

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved.