1

How i can migrate this code to Lightning web component:

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

                );
            </script>

Apex Method:

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

Base publication: OutputText value calling a @readOnly method

closed as off-topic by Martin Lezer, Raul, glls, Mohith Shrivastava, Jayant Das May 28 at 13:05

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on problems in code you've written must describe the specific problem and include valid code to reproduce it. For help writing short, self-contained syntactically-valid examples, see: SSCCE.org" – Martin Lezer, Raul, glls, Mohith Shrivastava, Jayant Das
If this question can be reworded to fit the rules in the help center, please edit the question.

3

Take a look at the wire method examples with apex methods here

Basically, would make the apex method aura enabled by adding:

@AuraEnabled(cacheable=true)

You would 'wire' that method to the component via import e.g.

import getRemoteTotalAccounts from '@salesforce/apex/YOUR_CONTROLLER_NAME.getRemoteTotalAccounts';

And then in the LWC, the wire method will bind the result to a property/function that you can expose on the page

 @wire(getRemoteTotalAccounts ) contactcount;
  • If I need more than 50 thousand records the @wire will bring them without problem? – Alex M. May 27 at 12:31
  • That I don't know - as far as I know, the limits with aura cacheable are the same as they were with VF, but not sure if the VF ReadOnly mode is the same with cacheable – BritishBoyinDC May 27 at 12:54
  • @AlexM. It's irrelevant in recent releases; the way COUNT works has been changed. – sfdcfox May 27 at 14:06
0

You can use apex method directly, no need to use remote action.

Write @auraEnabled method in Apex.

@AuraEnabled(cacheable=true)
public static Integer getRemoteTotalAccounts() {
    return [Select count() FROM Account];
}

Import method in Lwc.

import getRemoteTotalAccounts from '@salesforce/apex/Classname.getRemoteTotalAccounts';

Wire method in js.

@wire(getRemoteTotalAccounts) result;

Use result in HTML like {result.data};

Not the answer you're looking for? Browse other questions tagged or ask your own question.