+ Start a Discussion

How to update more than 1 lacs records using batch class by API call.

Hi Guyz,

I've written a batch class for updating records and its working for 10000 records. I've to update more than 1 lacs records using API call and as I know the JSON limit is 10000. So can anyone help me out for solving this issue? How to pass multiple calls one by one? Please check my code and suggest me for solution. Thanks in advance.
global class AccountDataSyncBatch implements Database.Batchable<sObject>, Database.AllowsCallouts, Schedulable, Database.stateful{
   global String lastAccountId;
   global String lastContactId;
   global String lastOpportunityId; 
   global boolean isStart;
   global AccountDataSyncBatch(){
      /* lastAccountId = accId;
       lastContactId = conId;
       lastOpportunityId = oppId;
       isStart = tempStart; */
   global Database.QueryLocator start(Database.BatchableContext BC){
      //return Database.getQueryLocator('SELECT Id FROM User LIMIT 1');
      string query = 'SELECT Id, name, Account_Lead_Score__c,Relative_Performance__c, (select id, name,Contact_Lead_Score__c from contacts), (select id, name, Opportunity_Lead_Score__c from opportunities) from account';
      return Database.getQueryLocator(query);

   global void execute(Database.BatchableContext BC, List<sObject> scope){
    global void fetchData(){
          //navikAuthDomain.response mapResp = navikAuthentication.getMapId(UserInfo.getUserEmail());
            Http http = new Http();
            HttpRequest request = new HttpRequest();
         // request.setHeader('sessionToken',mapResp.sessionToken);
            request.setHeader('sessionToken', 'UNUEXHA0//SLWpBR9J3v6gEN5hHsbQd0C79nFkpelRojiBfhF6Yth2dZi6oawXSRYDE6AFRGsN0gpV3N0XMrqAkynQ5ZWSmcGT3QAomh3f0=');
            if(isStart = true){
            HttpResponse response = http.send(request);
            if (response.getStatusCode() == 200) {
                DataSyncBatchHelper.ParentData parentData = (DataSyncBatchHelper.ParentData)JSON.deserialize(response.getBody(), DataSyncBatchHelper.ParentData.class);
                if(parentData != null && parentData.data != null){
                    List<Account> accList = new List<Account>();
                    List<Contact> conList = new List<Contact>();
                    List<Opportunity> oppList = new List<Opportunity>();
                    if(parentData.data.accounts != null && parentData.data.accounts.size() > 0){
                        for(DataSyncBatchHelper.Accounts account : parentData.data.accounts){
                            if(account.accountNumber != null && account.accountNumber.startsWith('001')){
                                accList.add(new Account(Id = account.accountNumber,
                                                                 Account_Lead_Score__c = account.accountleadScore,
                                                                 Relative_Performance__c = account.relative_Performance));
                        if(accList.size() > 0){
                        Database.update(accList, false);
                      if(parentData.data.contacts != null && parentData.data.contacts.size() > 0){
                        for(DataSyncBatchHelper.Contacts contact : parentData.data.contacts){
                            if(contact.contactId != null && contact.contactId.startsWith('003')){
                                conList.add(new Contact(Id = contact.contactId,
                                                                 Contact_Lead_Score__c = Decimal.valueOf(contact.contactLeadScore)));
                          if(conList.size() > 0){
                        Database.update(conList, false);
                       if(parentData.data.opportunities != null && parentData.data.opportunities.size() > 0){
                        for(DataSyncBatchHelper.Opportunities opportunity : parentData.data.opportunities){
                            if(opportunity.opportunityId != null && opportunity.opportunityId.startsWith('006')){
                                oppList.add(new Opportunity(Id = opportunity.opportunityId,
                                                                          Opportunity_Lead_Score__c = Decimal.valueOf(opportunity.Opportunity_Score)));
                         if(oppList.size() > 0){
                        Database.update(oppList, false);
   global void finish(Database.BatchableContext BC){
    global void execute(SchedulableContext sc){
      database.executebatch(new AccountDataSyncBatch());

Hi Rajan, 

Can you please explain more where is the limit you are setting is it in query? 

Hi JS,

Thank you for quick response. I've not given any limit in query because their is no data limit and it wil be keep changing. As per my understanding their is 10000 records limit in one api call so I've given limit in JSON received by third party and they are giving 9000 records in one hit and I'm able to update that 9000 records. But what I have to change for more records for which my API will hit till the final record. PLease check my code and suggest me what should i do in this case or if any code reference. Thanks in advance.