+ Start a Discussion
Gm ChouhanGm Chouhan 

Nested Batch Apex Calling

i have writed 2 batch apex
--------------------------
1 st
-------------------
global class AccountBatchApex1 implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        System.debug('Account Batch Apex1 Staring...........111111111111111')
        String query='SELECT Phone FROM Account limit 1 order by desc';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc,List<Account> scope){
        for(Account a:scope){
            a.phone+=1;  
        }
        update scope;
    }
    global void finish(Database.BatchableContext bc){
        System.debug('Ok Account Batch Apex1 fineshed.........11111111111111111');
        AccountBatchApex2 aba2 = new AccountBatchApex2();
        Database.executeBatch(aba2);
    }

}
-----------------------------------
2nd
---------------------------
global class AccountBatchApex2 implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        System.debug('Account Batch Apex2 Staring...........2222222222222222')
        String query='SELECT name, Phone FROM Account limit 1 order by desc';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc,List<Account> scope){
        for(Account a:scope){
            a.phone+=1;              
        }
        
        update scope;
      
    }
    global void finish(Database.BatchableContext bc){
        System.debug('OK Account Batch Apex 2 finished.........2222222222');
        AccountBatchApex1 aba1 = new AccountBatchApex1();
        Database.executeBatch(aba1);
    }

}
--------------------------
phone no = 10
but  no method execution......pls help
Best Answer chosen by Gm Chouhan
Ramesh KallooriRamesh Kalloori
find the updated code.

change the query to:SELECT name,Phone FROM Account order by Lastmodifieddate limit 1
global class AccountBatchApex1 implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        System.debug('Account Batch Apex1 Staring...........111111111111111');
        String query='SELECT name,Phone FROM Account order by Lastmodifieddate limit 1';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc,List<Account> scope){
        for(Account a:scope){
            a.phone+=1;  
        }
        update scope;
    }
    global void finish(Database.BatchableContext bc){
        System.debug('Ok Account Batch Apex1 fineshed.........11111111111111111');
        AccountBatchApex2 aba2 = new AccountBatchApex2();
        Database.executeBatch(aba2);
    }

}
global class AccountBatchApex2 implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        System.debug('Account Batch Apex2 Staring...........2222222222222222');
        String query='SELECT name,Phone FROM Account order by Lastmodifieddate limit 1';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc,List<Account> scope){
        for(Account a:scope){
            a.phone+=1;              
        }
        
        update scope;
      
    }
    global void finish(Database.BatchableContext bc){
        System.debug('OK Account Batch Apex 2 finished.........2222222222');
        AccountBatchApex1 aba1 = new AccountBatchApex1();
        Database.executeBatch(aba1);
    }

}


thanks,
RAmesh

All Answers

Vinit_KumarVinit_Kumar
How are you executing these batch apex classes ??

Ramesh KallooriRamesh Kalloori
find the updated code.

change the query to:SELECT name,Phone FROM Account order by Lastmodifieddate limit 1
global class AccountBatchApex1 implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        System.debug('Account Batch Apex1 Staring...........111111111111111');
        String query='SELECT name,Phone FROM Account order by Lastmodifieddate limit 1';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc,List<Account> scope){
        for(Account a:scope){
            a.phone+=1;  
        }
        update scope;
    }
    global void finish(Database.BatchableContext bc){
        System.debug('Ok Account Batch Apex1 fineshed.........11111111111111111');
        AccountBatchApex2 aba2 = new AccountBatchApex2();
        Database.executeBatch(aba2);
    }

}
global class AccountBatchApex2 implements Database.Batchable<sObject>{
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
        System.debug('Account Batch Apex2 Staring...........2222222222222222');
        String query='SELECT name,Phone FROM Account order by Lastmodifieddate limit 1';
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc,List<Account> scope){
        for(Account a:scope){
            a.phone+=1;              
        }
        
        update scope;
      
    }
    global void finish(Database.BatchableContext bc){
        System.debug('OK Account Batch Apex 2 finished.........2222222222');
        AccountBatchApex1 aba1 = new AccountBatchApex1();
        Database.executeBatch(aba1);
    }

}


thanks,
RAmesh
This was selected as the best answer
Vinit_KumarVinit_Kumar
Just saw your query is not correct ,

Change your query from 

String query='SELECT Phone FROM Account limit 1 order by desc';

to

String query='SELECT Phone,CreatedDate FROM Account order by CreatedDate limit 1';

This should work !!

If this helps,please mark it as best answer to help others :)