You need to sign in to do that
Don't have an account?
Lee Sin
Avoid Too Many Query Rows
Can anyone teach me how to fix this query?
List<Account> advisors=[select id,client_territories__c,name from Account where ......];
for(Account a : advisors)
{
List<Account> a1=[select id from Account where client_territories__c=:a.client_territories__c];
..........
........
}
I want to avoid wrting queries in For Loop.
So I want to fix the query this way:
List<Account> advisors=[select id,client_territories__c,name,[select id from Account where client_territories__c=?????] from Account where ......];
I'm stucked here.
Set<String> territorySet = new Set<String>();
List<Account> advisors=[select id,client_territories__c,name from Account where ......];
for(Account a : advisors)
{
territorySet.add(a.client_territories__c);
..........
........
}
List<Account> a1=[select id from Account where client_territories__c in :territorySet];
All Answers
Set<String> territorySet = new Set<String>();
List<Account> advisors=[select id,client_territories__c,name from Account where ......];
for(Account a : advisors)
{
territorySet.add(a.client_territories__c);
..........
........
}
List<Account> a1=[select id from Account where client_territories__c in :territorySet];
What I want to do is similar to this: https://developer.salesforce.com/page/Best_Practice:_Avoid_SOQL_Queries_Inside_FOR_Loops
But the example above does not have a where clause in the nested select statement.
How can I add a where clause?
You would need to take the query out of the loop for sure, and the only way to do it is to collect all conditions and take out records at one time (my last query).
I have just stated an example, but you need to add your own where condition to this query. And, I am assuming that the where condition is common and is not changing with each iteration of advisors loop.
Once you have the records from second query, you would then need to run another loop and then process the accounts as per your business rules.
This could be the logic:
I'm afraid without knowing the business logic, I wont' be able to give you the exact solution, but hope this helps.
So this can help me avoid the SOQL query limit.
Can I ask one more thing? Once I used the where in cluse, I got another error which is the too many rows 50001.
Does this mean I have to use batch apex?
But I checked batch apex and it is mostly used for DML operations.
If I hit the rows limit in my query can I also use batch apex?
The reason is, that Salesforce has a hard limit of 50,000 records per Apex Transaction, but for batch apex, the limit is reset after every execute method (unless you are using stateful). Usually any transaction in which you are processing more than 50k records can bedone in batch transaction