You need to sign in to do that
Don't have an account?
dJadhav
To Many SOQL:101
Hi ,
In my application, I want to write scheduler.The code from schduler is executing for each user from organization.
For each user, I have to execute arrount 20 DML +SOQL statements.
I have more than 100 user so getting Limit exception :Too many SOQL :101
Can anyone help?
Thanks in advance.
Regards,
Dipak
It seems you are just having the scheduler class and you are processing the data for all users in that scheduler execute method. And therefore hitting the 101 SOQL limit.
You need to create another class which implements the Batchable interface and you need instantiate this batch class and pass the query for users to the executeBatch method which will take 2 parameters i.e. the Batch class instance and query. Something like this below:
global void execute(SchedulableContext SC) {
//Fetching all Users
String query = 'Select Id from User';
//Passing SQL to Batch job
BatchClass job = new BatchClass (query);
//Calling Batch job for 1 user
ID batchprocessid = Database.executeBatch(job, 1);
}
Check the following link for an example of batch job and its structure.
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
Hope this helps.
Hi ,
Thanks for the reply.
I have written my code in batch and tried to test it. I am getting exception as Too Many SOQL:201
My Code:
Can you please tell me the better way to write Batch job? Also tell me , how may times i can run the Sceduler in a day?
Thanks,
Dipak
google "apex best practices" and 'governor limits". You have to use lists, maps, sets to group soql queries. A general illustration of the concept where the requirement is finding an owner on an account trigger:
for(account a: Trigger.new)
{
user u = [select u.id from user u where u.id =: a.ownerid];
}
results in lots of queries. It should be:
set<id> owners = new set<id>();
for(account a: Trigger.new) owners.add(a.ownerid);
user[] users = [select u.id from user u where u.id in: owners];
//now find the individual user for the account programmatically.
Can you paste the complete code. As overall things looks fine in the above code. So the exception coming could really be because of the code which is shown as comments here.
Hi,
Thanks for reply.
I am changing my logic to get events for user. In execute method of Batch , I have moved the logic to Scheduler. I am testing this logic.
Thanks,
Dipak