You need to sign in to do that
Don't have an account?

Too Many SOQL Error when deleting tasks from opportunity
Hello ! i am developing this code to delete tasks from previous opportunity's the problem in this code is this gives too many soql error because i have 1000's of opportunity's to work is there a way i can do this i mean can i delete tasks from opportunity's based on lastmodified date or all at once please help
global class SalesOrderOpportunityTasks implements Schedulable{ global static String CRON_EXP = '0 0 8 * * ? *'; global void execute(SchedulableContext sc) { doOpportunityUpdates(); } global void doOpportunityUpdates() { List<Opportunity> opps = [Select Id,StageName,Name From Opportunity Where LastModifiedDate > = 2015-11-27T00:00:00Z and LastModifiedDate < = 2015-12-01T00:00:00Z Limit 33]; List<Sales_Order__c> so = new List<Sales_Order__c>(); for (Opportunity o : opps) { System.debug('Debug Opportunity :' + o.Id +'----' + o.Name); so = [SELECT Id,OrderNum__c,Quote__c FROM Sales_Order__c WHERE Quote__c = :o.Id]; } for(Sales_Order__c s :so) { if(s.OrderNum__c != Null) { if(s.OrderNum__c == 'QUOTE WON' || s.OrderNum__c == 'QUOTE LOST' || s.OrderNum__c.charAt(0) == 67 || s.OrderNum__c.charAt(0) == 99) { List<Task> deleteTasks = [SELECT Id, Type, Status, WhatId FROM Task WHERE WhatId = :s.Quote__c]; System.debug('Deug Tasks' +deleteTasks); System.debug(s.Quote__c); if(!deleteTasks.isEmpty()) { System.debug('Mon' + deleteTasks); delete deleteTasks; } } } } } }
You can do this by utilizing maps, as in my example below. Note, my example is very simplified and may not even compile, but should get you in the right direction.
Also, please read the following blog post which highlights how to work within Salesforce governor limits and other Apex best practices: https://developer.salesforce.com/blogs/developer-relations/2015/01/apex-best-practices-15-apex-commandments.html
Try with below code ,You are getting this error as you have query inside loop .
Just check the relationship name what I have added in query may be it is diffreent in your object .
Also I am not sure why you have added LIMIT 33 in opportunity query .Also logic I kept same as your code .
Let me know if it helps !!
Thanks
Manoj
Try with below code it will hep !!
Thanks
Manoj
@James Loghry @Manoj Thank You both of you i created new code by understanding both of your codes and it worked Cheers!