0

I am doing an inbound Integration,where I am using HTTPGet method and inserting those records in Salesforce. Below is the code. listjsonFormatClass is the list of the Wrapper Class where i am sending the response and deserializing it. accountList is the list of Accounts which I am getting in the Start method of the batch after running this query:

select id,CustomerId__c,name,lastmodifieddate from Account

I am using this batchclass with implementing the Database.Allowscallouts as well.

 for(jsonFormatClass jsonFormatClassObject : listjsonFormatClass){
                    for(Account accObject : accountList){
                        if(accObject.TestId__c!= String.ValueOf(jsonFormatClassObject.testID)){
                            accountObj = new Account();
                            accountObj.TestId__c= String.ValueOf(jsonFormatClassObject.testID);
                            accountObj.Name = jsonFormatClassObject.Name;
                            accountToBeInsertedList.add(accountObj);                        
                        }
                        else{
                             accountObj = new Account();
                            accountObj.TestId__c= String.ValueOf(jsonFormatClassObject.testID);
                            accountObj.Name = jsonFormatClassObject.Name;
                            accountToBeUpdatedList.add(accountObj);
                        }
                    }
                }
insert accountToBeInsertedList;
  • can you paste the error here? – rahul gawale Jul 1 '19 at 7:31
  • Error in debug - Too many DML rows: 10001 – Aditya Jul 1 '19 at 7:32
  • Have you tried reducing the batch scope size from the default of 200 to say 20? Perhaps your insert is causing trigger and other logic to fire that is consuming DML statements. The general idea is that while start might return a large number of records, each execute can be set to process a small number of records per invocation. – Keith C Jul 1 '19 at 7:35
  • This is a new sandbox. No other logic is present.This is the only batch class in the whole sandbox. Its my own developer sandbox. I have tried to do 20,10,1 every possible scope.I have tried to do this without the batch as well I am facing the same error there too. – Aditya Jul 1 '19 at 7:39
  • 1
    @Aditya, The problem is, you are running using nested for loops. Lets say listjsonFormatClass has 5000 records and accountList has 3 records. So for each jsonFormatClassObject, total of three accounts will be created, so the final total will be 5000 * 3 i.e. 15000. If I am not wrong you want only one account per jsonFormatClassObject. – rahul gawale Jul 1 '19 at 7:55
3

For this type of scenarios, you can always use maps. I am writing some pseudo code below.

// After you query records, create a map of accounts by TestId__c
Map<String,Account> accountMap = new Map<String, Account>();
for(Account accountObj: accountList){
    accountMap.put(accountObj.TestId__c, accountObj); // assuming testid being unique for each account.
}

Then you need to iterate over the list you getting from the response.

for(jsonFormatClass jsonFormatClassObject : listjsonFormatClass){
    String testIdStr = String.ValueOf(jsonFormatClassObject.testID);
    Account accountObj;
    // if its existing account, it make sense to update it instead of creating new.
    if(accountMap.containsKey(testIdStr)){
        accountObj = accountMap.get(testIdStr);
        accountObj.TestId__c= String.ValueOf(jsonFormatClassObject.testID);
        accountObj.Name = jsonFormatClassObject.Name;
        accountToBeInsertedList.add(accountObj);         
    } else {
        // create new account.
        accountObj = new Account();
        accountObj.TestId__c= String.ValueOf(testIdStr);
        accountObj.Name = jsonFormatClassObject.Name;
        accountToBeUpdatedList.add(accountObj);
    }
}
// instead of inserting new you can update existing. So I would recommend to use Upsert.
upsert newAccounts;

To avoid governor limits and to write an optimized code you can learn more about the best practices here. As a beginner, this will be really helpful for you.

Happy coding!!

  • Hi @Rahul, Thanks for your help. – Aditya Jul 1 '19 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.