We know that salesforce works on the concept of Multi-Tenancy, because of which there are many governor limits that are in place to use the salesforce server resources smoothly. To check the limitations of in apex class as per specific scenario, we can make use of standard limit class.
The Limits methods return the specific limit for the particular governor, such as
- the number of calls of a method
- the amount of heap size remaining.
- Query Limits
- Queueable Jobs
- Number of API calls
- Limits on DML statements
And many more.
In this post, we will see how we can use limit class in a SOQL query to fetch maximum number of rows.
We know that salesforce fetched maximum of 50,000 rows. We also know that it is a bad way of programming to hardcode any value.
Public static List<Contact> m_Get_Contact_Records(){
List<Contact> l_Contacts = [SELECT Id,
Name,
Email
FROM Contact
LIMIT :(Limits.getLimitQueryRows() - Limits.getQueryRows())]; // This will return max number of records as per limit.
system.debug('-=-= Limits.getLimitQueryRows(): '+Limits.getLimitQueryRows());
system.debug('-=-= Limits.getQueryRows(): '+Limits.getQueryRows());
system.debug('-=-= Limits.getAggregateQueries(): '+Limits.getAggregateQueries());
system.debug('-=-= Limits.getLimitAggregateQueries(): '+Limits.getLimitAggregateQueries());
system.debug('-=-= Limits.getQueries(): '+Limits.getQueries());
system.debug('-=-= Limits.getLimitQueries(): '+Limits.getLimitQueries());
system.debug('-=-= Limits.getQueries(): '+Limits.getQueries());
return l_Contacts;
}
Output
-=-= Limits.getLimitQueryRows(): 50000
-=-= Limits.getQueryRows(): 41
-=-= Limits.getAggregateQueries(): 0
-=-= Limits.getLimitAggregateQueries(): 300
-=-= Limits.getQueries(): 1
-=-= Limits.getLimitQueries(): 100
Resources