0

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

Thanks

  • 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. – Caspar Harmer Jun 28 '17 at 0:00
3

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.

|improve this answer|||||

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.