+ Start a Discussion
SushupsiSushupsi 

Batch Apex Issue--Urgent

Hi All,

 

Thank you to all the replies first of all.

 

:smileysad:

 

I have written a Batchable Class. Is this also called a BULK API ?

 

The entire program is working fine except an anomolous behavior. I have tried to run the class for an upload of 10000 records, when the entire batch run it updated only 1700 records. rest all records were not updated.

 

Below is my program:

 

global class Get_Account_Id_EPC implements Database.Batchable<sObject>
{
    Public String Query;

    Global  database.querylocator start(Database.BatchableContext BC){
        Return database.getquerylocator(query);
    }

    Global void execute(Database.BatchableContext BC, List<SObject> scope)
    {
        List<Express_Payment__c> updatelist = new List<Express_Payment__c>();
        List<Express_Payment__c> epsobj = [Select  id,Agent__c,BP_ID__c,BP_Loc_Id__c from Express_Payment__c limit 1000];

        for(Express_Payment__c records : epsobj){
            for(sObject s : scope){
                Account a = (Account)s;
                if(records.BP_ID__c!=null && records.BP_loc_ID__c!=null&&records.BP_ID__c == a.BPID__c && records.BP_Loc_Id__c == a.BP_Loc_Id__c){
                    records.Agent__c = a.id;
                    updatelist.add(records);
                    break;
                }
            }
        }
        update(updatelist);
        
        List<Utility_Bill_Payment__c> updatelist1 = new List<Utility_Bill_Payment__c>();
        List<Utility_Bill_Payment__c> ubpobj = [Select  id,Agent__c,BP_ID__c,BP_Loc_Id__c from Utility_Bill_Payment__c limit 1000];

        for(Utility_Bill_Payment__c records : ubpobj){
            for(sObject s : scope){
                Account b = (Account)s;
                if(records.BP_ID__c!=null && records.BP_loc_ID__c!=null&&records.BP_ID__c == b.BPID__c && records.BP_Loc_Id__c == b.BP_Loc_Id__c){
                    records.Agent__c = b.id;
                    updatelist1.add(records);
                    break;
                }
            }
        }
        update(updatelist1);
        
        List<PrePaid_Card__c> updatelist2 = new List<PrePaid_Card__c>();
        List<PrePaid_Card__c> ppcobj = [Select  id,Agent__c,BP_ID__c,BP_Loc_Id__c from PrePaid_Card__c limit 1000];

        for(PrePaid_Card__c records : ppcobj){
            for(sObject s : scope){
                Account c = (Account)s;
                if(records.BP_ID__c!=null && records.BP_loc_ID__c!=null&&records.BP_ID__c == c.BPID__c && records.BP_Loc_Id__c == c.BP_Loc_Id__c){
                    records.Agent__c = c.id;
                    updatelist2.add(records);
                    break;
                }
            }
        }
        update(updatelist2);
        
        List<MT_Volume_Reports__c> updatelist3 = new List<MT_Volume_Reports__c>();
        List<MT_Volume_Reports__c> mtannualobj = [Select  id,Agent__c,BPID__c,BP_Loc_Id__c from MT_Volume_Reports__c limit 1000];

        for(MT_Volume_Reports__c records : mtannualobj){
            for(sObject s : scope){
                Account d = (Account)s;
                if(records.BPID__c!=null && records.BP_loc_ID__c!=null&&records.BPID__c == d.BPID__c && records.BP_Loc_Id__c == d.BP_Loc_Id__c){
                    records.Agent__c = d.id;
                    updatelist3.add(records);
                    break;
                }
            }
        }
        update(updatelist3);
        
        List<MT_Volume__c> updatelist4 = new List<MT_Volume__c>();
        List<MT_Volume__c> mtobj = [Select  id,Agent__c,BPID__c,BP_Loc_Id__c from MT_Volume__c limit 1000];

        for(MT_Volume__c records : mtobj){
            for(sObject s : scope){
                Account e = (Account)s;
                if(records.BPID__c!=null && records.BP_loc_ID__c!=null&&records.BPID__c == e.BPID__c && records.BP_Loc_Id__c == e.BP_Loc_Id__c){
                    records.Agent__c = e.id;
                    updatelist4.add(records);
                    break;
                }
            }
        }
        update(updatelist4);
        
        List<MO_Volume__c> updatelist5 = new List<MO_Volume__c>();
        List<MO_Volume__c> moobj = [Select  id,Agent__c,BP_ID__c,BP_Loc_Id__c from MO_Volume__c limit 1000];
        for(MO_Volume__c records : moobj){
            for(sObject s : scope){
                Account f = (Account)s;
                if(records.BP_ID__c!=null && records.BP_loc_ID__c!=null&&records.BP_ID__c == f.BPID__c && records.BP_Loc_Id__c == f.BP_Loc_Id__c){
                    records.Agent__c = f.id;
                    updatelist5.add(records);
                    break;
                }
            }
        }
        update(updatelist5);
    }

    Global void finish(Database.BatchableContext info){
    }

    /* Test Coverage Class*/
    Public static testMethod void SEUpdateTest()
    {
        User currentUser = [Select u.Id From User u Where u.Id =: UserInfo.getUserID()];
        List<Account> testAccs = new List<Account>();
        for(integer i=0;i<10;i++)
        {
            Account testAccount = new Account(Name = 'TestAccount'+i, OwnerId = currentUser.Id);
            testAccs.add(testAccount);
        }
        insert testAccs;
        Test.starttest();
        Get_Account_Id_EPC sObj = new Get_Account_Id_EPC();
        sObj.Query = 'Select id,BPID__c,BP_Loc_Id__c from Account';
        ID BatchId = Database.executeBatch(sObj,500);
        test.stoptest();
        AsyncApexJob a = [Select Id, Status, NumberOfErrors, JobItemsProcessed, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id = :BatchId];
        System.debug('\n\nFinal results are: '+a);
        //System.AssertEquals('Completed', a.status);
        //System.AssertEquals(0, a.NumberOfErrors);
    }

}

 In the above class six objects are updated using the query in the start method. But for every execution all the six loops are executed. When i have checked the "Apex Jobs" in "Monitoring section " (Administration Setup->Monitoring->Apex Jobs) i have found that Total batches was "133" and for any single update entire batch was running. Please explain me this concept and also my specific question is below:

 

1.) Why is that my batch of 10,000 records did'nt get updated rather only 1700 were processed. Is there any other way of setting the limit.

2.) Is there any thing that has to be changed in the above code so that all the records get updated in bulk like setting the batch size.

 

TIA

- Sushupsi