When we are running the below batch getting:

Too many SOQL queries: 201 Error.

What are some steps I can take to avoid this error?

Batch: Will post the code.Sorry for the deletion


  • You should refactor this class. It's a terrible wall of code and will continue to cause you problems until you break it out into some more discrete functionality. Jun 28, 2017 at 0:00

1 Answer 1


Sorry to say I didn't read the whole code (too long), but with a quick glance I found this query inside a for(Opportunity opp : newOppListfromOpp) loop:

assetsList = [Select id,ParentId,Service_Contract__c,type__c,GlobalCare_Level__c,Asset_Value__c,smart_part_number__c from Asset where AccountId in :accList and GlobalCare_Level__c!='None' and GlobalCare_Level__c!='' and Type_of_Key__c != 'Demo' and Status = 'Active'];

You should never have SOQL queries inside loops. Fix that one and look for other occurrences, that is very much likely to be the cause of your Exception. The alternative is to make that query outside of a loop only once, and you can make a Map<Id, SObject> to map the queried objects and be able access them inside the loop later, instead of making the query there.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .