Book Image

Learning Apex Programming

5 (1)
Book Image

Learning Apex Programming

5 (1)

Overview of this book

Table of Contents (17 chapters)
Learning Apex Programming
Credits
Foreword
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Techniques to avoid query limits


If you have ever been responsible for code to meet business requirements, then you probably know that decision-makers change their minds and requirements change over time. For this reason, it is a best practice to store your constant variables (or should we call them not so constant) as data rather than hardcoding them in your Apex classes. In order for your code to access those constants, you would have to query them. Let's suppose that our code needs to know the phone number for the company we work for, as well as our company's parent company. We could write code to query the Account records as shown in the following code block:

Account ourCompany = [
Select Id, Phone 
from Account 
where Name = 'Acme National' 
limit 1
];
Account ourParentCompany = [
Select Id, Phone 
from Account 
where Name = 'Acme International' 
limit 1
];

While the previous code is simple enough to write, it uses two of our allowed queries and doesn't follow our best practice of being...