Working with Large Sets of Data
Visualforce custom controllers and controller extensions are subject to Apex governor limits. For more information about governor limits, see Execution Governors and Limits. Additionally, Visualforce iteration components, such as <apex:pageBlockTable> and <apex:repeat>, are limited to a maximum of 1,000 items in the collection they iterate over.
Sometimes your Visualforce pages may need to work with or display larger sets of data, but not need to make modifications to that data; for example, if you are providing custom reporting and analytics. Visualforce offers developers a “read-only mode”, which relaxes the limit on the number of rows which can be queried in one request, and increases the limit on the number of collection items which can be iterated over within the page.
You can specify read-only mode either for an entire page or, with certain limitations, on individual components or methods.
Note -
You can only iterate over large sets of data if you specify read-only mode for the entire page.
Setting Read-Only Mode for an Entire Page
Normally, queries for a single Visualforce page request may not retrieve more than 50,000 rows. In read-only mode, this limit is relaxed to allow querying up to 1,000,000 rows.
To enable read-only mode for an entire page, set the readOnly attribute on the <apex:page> component to true.
<apex:page controller="SummaryStatsController" readOnly="true">
<p>Here is a statistic: {!veryLargeSummaryStat}</p>
</apex:page>
public class SummaryStatsController {
public Integer getVeryLargeSummaryStat() {
Integer closedOpportunityStats =
[SELECT COUNT() FROM Opportunity WHERE Opportunity.IsClosed = true];
return closedOpportunityStats;
}
}
Setting Read-Only Mode for Controller Methods
Visualforce controller methods can, with some important limitations, use the Apex ReadOnly annotation, even if the page itself isn’t in read-only mode.
In Apex, the @ReadOnly annotation is used to specify that a method or class is read-only, meaning it does not perform any DML (Data Manipulation Language) operations like insert, update, delete, or undelete. This annotation helps in enforcing best practices and preventing accidental modifications to data within certain methods or classes, especially when dealing with complex logic or integration scenarios.
@ReadOnlypublic class MyReadOnlyClass {
public void fetchRecords() {
// Logic to fetch records without performing any DML operations
List<Account> accounts = [SELECT Id, Name FROM Account];
// No DML operations allowed within this method
}
public void someOtherMethod() {
// Other methods in the class
}
}
Comments
Post a Comment