function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

How to Use @readOnly Annotation in Apex Class in Salesforce

Hello Everyone,


How to Use @readOnly Annotation in Apex Class. Can anyone suggest me by sending one sample class with @readOnly Annotation.


Please let us know if we use @readOnly annotation in Apex Class that is calling from Apex Trigger.




Readonly annotation is used for methods. Try using them over a method it should work
Bhawani SharmaBhawani Sharma
Basically we use this where VF page comes in picture and you want to display a list of records on page.
By default if if you are using a list on VF page, that can not hold more than 1000 records. but @readOnly increases this limit to 10,000. But you can not perform any dml operation on this list.
anusha c 8anusha c 8
Sidharthn NegiSidharthn Negi
This is pretty late here. But just for future reference:

@Readonly annotation is used in Apex only and is used to increase the SOQL limit from 50k to million. You add the annotation over a method and the limits are relaxed for that method only. However you cannot perform any DML within it.

Readonly attribute is used in Visualforce page (readonly="true") and is only used to relax the limit of showing list records from 1,000 to 10,000.
This also prevents edits on these records.

Both are different from each other and CAN be used in different scenarios.

Please let us know if we use @readOnly annotation in Apex Class that is calling from Apex Trigger.
This is a rare scenario but you can use @Readonly on apex trigger handlers to query say 200k records, but to update them you would need to use @future and use counters to run multiple jobs updating 10k records each. I have't tried this but seems to be possible.

Please mark this as best answer.