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

2 Answers 2

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;
3
  • If I need more than 50 thousand records the @wire will bring them without problem?
    – Alex M.
    May 27, 2019 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 May 27, 2019 at 12:54
  • @AlexM. It's irrelevant in recent releases; the way COUNT works has been changed.
    – sfdcfox
    May 27, 2019 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 .