Another very common governor limit is SOQL LIMIT. There are many best practices to be followed while writing efficient code. Here, is one of the link:

In this blog, I will not be explaining the best practices but I will be throwing some pointers what needs to be considered when SOQL limit is reaching:

  1. Check the debug logs and find out the SOQL limit usage and note the list of triggers, classes which have been executed so far.
  2. Verify code – Is there any multiple SOQL statements running on the same object?
    • If Yes, then make it one and segregates it using collections and then process it
  3. Cross-verify the SOQL queries are not used inside loop.
  4. Any SOQL statement for getting Recordtype?
    • If Yes, then remove it and replace with below sample code:Id recordTypeId = Schema.SObjectType.Account.RecordTypeInfosByName.get(‘RecordTypeName’).RecordTypeId;replace Account with the relevant object API name
  5. SOQL for Custom Setting?
  6. Certified Managed Package SOQL queries Limit has their own limit (100), in addition to the native code limit.
  7. Revisit all the SOQL queries and try to write it more efficiently:

Below are few scenarios:

Scenario 1: A SOQL query executing in multiple times say in Method1 and Method2 in a Trigger:

Solution: Lets say, “performOp1” is getting called in afterUpdate event of Opportunity developed by “X” developer. And now, there is new business requirement on which developer “Y” is working.


public class OpportunityTriggerHandler {
public static void afterUpdateProcess(List<Opportunity> newOpplist, Map<Id, Opportunity> oldOppMap){
performOp1(newOpplist, oldOppMap); // already existing method
performOp2(newOpplist, oldOppMap); // New method added
}

private static void performOp1(List<Opportunity> newOpplist, Map<Id, Opportunity> oldOppMap){
List<OpportunityLineItem> opplitemlist = [Select id,Name from OpportunityLineItem where Opportunityid IN: newOpplist];
/*
* business logic
*/
}

private static void performOp2(List<Opportunity> newOpplist, Map<Id, Opportunity> oldOppMap){
List<OpportunityLineItem> opplitemlist = [Select id,Name from OpportunityLineItem where Opportunityid IN: newOpplist];
/*
* business logic
*/
}
}

Now, this is the common mistake where developer doesn’t want to touch the existing code and they implement their own code.. this leads to increase in number of SOQL statements.

This can be resolved in multiple ways like performing the SOQL before “performOp1” is called and declaring “List<OpportunityLineItem> opplitemlist ” as static.


public class OpportunityTriggerHandler {
private static List<OpportunityLineItem> opplitemlist;
public static void afterUpdateProcess(List<Opportunity> newOpplist, Map<Id, Opportunity> oldOppMap){
opplitemlist  = [Select id,Name from OpportunityLineItem where Opportunityid IN: newOpplist];

performOp1(newOpplist, oldOppMap); // already existing method
performOp2(newOpplist, oldOppMap); // New method added
}

This actually helps in reducing the number of query rows.

Scenario 2: A SOQL query is used to query 2 related objects data

Solution: Using sub-queries


SELECT Account.Name, (SELECT Contact.LastName FROM Account.Contacts) FROM Account

Scenario 3: SOQL query performed for updating when list of IDs are available

Solution: If you want to update the set of records, there is no need for explicit query unless you need them for other processing.

You consider a scenario where you are updating the Account Name from the dataloader. In this, we will just have 2 columns – one accountId and another AccountName.

In similar fashion, we can implement it in code as well like –


List<Account> updateAcclist = new List<Account>();

updateAcclist.add(new Account(id='0012800000dxzxB', Name = 'Test Account 1');
updateAcclist.add(new Account(id='0012800000dxzxC', Name = 'Test Account 2');

update updateAcclist;

Please feel free to post your queries/thought.

Happy Coding!!!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s