In this blog, we will discuss the readOnly annotation in Apex. As we know Salesforce is a multitenant architecture; therefore, it enforces governor limit. It has various limits like DML Limit, SOQL limit, CPU limit, etc.
When it comes to the SOQL limit we know that a single transaction query returns only 50,000 records and not more than that, If we try to we will encounter Too many query rows: 50001.
Transitioning to the discussion of this annotation, it allows us to query 1 Million records in a less restrictive way against our Force.com database. However, it still enforces other limits.
Moreover, it only works with REST, SOAP web services and a Schedulable interface.
First, let us understand what are annotations and how are they helpful
What are annotations?
They provide a certain behaviour to our Apex classes. Like Java, In Apex annotations alter our functions and provide additional pieces of information to the compiler at run time.
Use the @ symbol followed by the appropriate keyword to specify them as annotations. We have to write it before method definitions
Example:
Here, by using @future we are executing our method asynchronously. Likewise, we have many other annotations that we can use to modify the behaviour of our Apex.
Following are the common annotations that we used in our Apex.
1. @future – To make any Apex class run asynchronously.
2. @isTest – Use this annotation to make an apex class or any apex method to run as a test. It defines the code containing this is only used for testing.
3. @AuraEnabled – It is used to make the method available for the Lightning Web Components and Aura Components. It enables client-side and server-side access to an Apex controller method.
4. @InvocableMethod – Invocable methods are called natively from Rest, Apex, Flow, or Einstein bot that interacts with the external API source. Invocable methods have dynamic input and output values and support describe calls.
5. @NamespaceAccessible – It makes apex public contains in a package to other packages with the same namespace. Without this, any method, abstract, interface, or property defined in a 2 GP will not be accessible to other packages with the same namespaces.
6. @RestResource – We use it to expose any Apex class as a REST resource.
7. @HttpDelete – We use it at the method level to expose it as REST resources. It will be called when sending an HTTP Delete request.
8. @HttpGet – We use it at the method level to expose it as REST resources.t will be called when an HTTP Get (retrieve) request is sent.
9. @HttpPut – We use it at the method level to expose it as REST resources. It will called when an HTTP Put (upsert) request is sent.
10. @HttpPost – We use it at the method level to expose it as REST resources. It will called when an HTTP Post (insert) request is sent.
11. @HttpPatch – We use it at the method level to expose it as REST resources. It will called when an HTTP Patch (update) request is sent.
What is readOnly annotation with examples
Let’s understand this new annotation by a basic example in which we will retrieve the Account records via the Schedulable interface. Use of this will be beneficial if we don’t have a requirement to perform any DML statement.
Things to remember while working with readOnly annotation
- It works for SOAP, REST Web Services and Schedulable Interface. if we try to implement it other than these we will get the below error. Hence, to use the @ReadOnly annotation, the top-level request must be in the schedule execution or the Web service invocation
- The request does not allow any DML Operations.
- Before API version 49.0, developers needed to annotate the Apex Rest method with @RemoteAction if they were using read-only annotations. However, after version 49.0, simply annotating it suffices; there’s no additional requirement for @RemoteAction.
Want to Learn Salesforce Flows? Checkout our Salesforce Flow Course
FAQs
1. What is 2GP?
It is a second-generation managed package. It is a new way of building packages with the SFDX cli command and version control.
2. What does @RemoteAction annotation do?
The RemoteAction annotation enables JavaScript to call Apex methods used in Visualforce. In VisualForce, this process often referred to as JavaScript remoting involves calling any method of the Apex controller through JavaScript methods. Additionally, it indicates that you can invoke any method of the Apex controller through JavaScript methods.
3. What is a record read-only error in Apex?
In Apex, you encounter the “System.FinalException: Record is read-only” error when you attempt to modify a record marked as read-only.
Also Read – Null Coalescing Operator in Salesforce
Conclusion
In conclusion, this annotation is a great way to retrieve records by passing its governor limit. But we still need to consider other limits that’s why we have to customize our code in a more optimized way.