Ad
Ad
Ad
Beginner Dev

Dev Tip 4: Solving too many SOQL Queries 101 errors

Pinterest LinkedIn Tumblr

In this post, we will try to understand what causes “Too many SOQL Queries 101 error”. Salesforce is a multi-tenant environment(Multiple client orgs are run on the Same server) as a result there are some limitations enforced by Salesforce to make sure all clients utilize the platform efficiently. As part of this process, Salesforce imposes Governor limits on its clients to make sure all the orgs adhere to salesforce guidelines.

Too many SOQL Queries 101 error is a governor limit imposed by Salesforce and this issue appears when we write our query inside for Loop. The maximum number of queries allowed in a Single transaction is 100 queries. The number 100 looks to be a huge number but we could hit this number very easily in a Single transaction if we do not write our code efficiently.

Inefficient Code: The below code cause “Too many SOQL Queries 101 error” when we load more than 100 records in a single transaction. Note: By default, triggers process 200 records at a time.

for(Contact con: trigger.new)

{

List<Account> AccountList =[Select id,Name from Account where id=:con.AccountId];

}

Efficient Code: Remove the Query inside the for loop and write it outside the loop.

List<String> AccidList =new List<String>();

for(Contact con: trigger.new)

{

AccidList.add(con.accountId);

}

List<Account> AccountList =[Select id,Name from Account where id=:AccidList];

Write A Comment