2

I want to display the total number of Accounts in an <apex:outputText>.

For that, I did :

VF :

<apex:outputText value="{!$totalAccounts}"/>

Controller :

public Integer getTotalAccounts() {
        return [Select count() FROM Account];
}

But I encounter the limit when I have more than 50000 Accounts.

So, I try to use the @readOnly keyword on my method but get this error on my IDE :

Only WebsService, RemoteAction, or Schedulable.execute() methods can be marked as read-only

The only way is to use the @RemoteAction but I don't know how to do that.

How can I call a @RemoteAction method to get my total Accounts number and then display it in my outputText ?

2 Answers 2

3

We use Javascript (with the AJAX Toolkit) to call a @RemoteAction method (must be static).

In your case, you can define a div in your visualforce page (instead of outputText -> no need to a getter in the controller) with a Javascript <script> like this (replace YOUR_CONTROLLER_NAME with the appropriate controller name):

<div id="totalAccounts"></div> 
                <script>
                Visualforce.remoting.Manager.invokeAction(
                    '{!$RemoteAction.YOUR_CONTROLLER_NAME.getRemoteTotalAccounts}',
                        function(result){
                            document.getElementById('totalAccounts').innerHTML = result;
                        }

                    );
                </script>

and in YOUR_CONTROLLER define this statis method:

@RemoteAction
@readOnly
public static Integer getRemoteTotalAccounts() {
     return [Select count() FROM Account];
}
0
0

If you are only displaying information at the VF page, you could switch it over to read only mode:

<apex:page controller="PageController" readOnly="true">
    <apex:outputText value="{!$totalAccounts}"/>
</apex:page>

This would relax the limits up to 1 million records.

1
  • No I do some DML operations in this page.
    – SF_user
    Sep 23, 2014 at 15:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .