0

When you want to call Apex class in visualforce page we have to declare our apex class in the following format.

<apex:page Controller="ClassName">

Whenever we call a visualforce page in which controller attribute is defined it will first create an object for the apex class which is defined in the controller.

I want to do the same in LWC how to do it?

2
  • Add import methodName from "@salesforce/apex/Controller.methodName";. In the Apex code specify @AuraEnabled(cacheable=false) (true or false for cacheable) on your method. Then call it anywhere in js file methodName().then(result => {}).catch(err => {});
    – zaitsman
    Commented Jul 2, 2020 at 7:26
  • I wanted to know like in Visualforce it will first call the constructor on above mentioned code <apex:page Controller="ClassName" > so how to call the same apex class constructor from the LWC.?
    – Varun
    Commented Jul 2, 2020 at 7:41

3 Answers 3

4

LWC can only invoke static Apex methods, via @wire and imperative calls. There's no instantiation of a "controller" - indeed, there is no "controller" for the LWC, just a (possibly distributed, possibly in one class) set of @AuraEnabled static methods for the LWC to invoke.

Wires are invoked shortly after component instantiation though this is delayed based on any parameters - the wire is invoked once all parameters have defined values. Imperatives are explicitly called from functions in your LWC.

The LWC doesn't rely on state "on the server side" and so must maintain and explicitly pass all required details to each wire or imperative call.

This is covered quite well in the documentation. Note also the "lifecycle hooks". You can also undertake the trailheads to find out more.

If you have code that you wish to share between a Visualforce page and an LWC you will need to refactor the code so it can be called through, or as, an @AuraEnabled static method and from your Visualforce controller.

Also understand that there is no automatically maintained "page state" shared between client and server in LWC (unlike Visualforce). Instead any state must be explicitly passed as parameters to the apex method, and explicitly updated on the client side on receipt of the responses from these calls. This makes for much more responsive pages.

5
  • I wanted to know like in Visualforce it will first call the constructor on above mentioned code <apex:page Controller="ClassName" > so how to call the same apex class constructor from the LWC.?
    – Varun
    Commented Jul 2, 2020 at 7:42
  • 1
    @Varun you can't
    – zaitsman
    Commented Jul 2, 2020 at 7:42
  • As I said, there's no controller and LWC can only invoke static methods.
    – Phil W
    Commented Jul 2, 2020 at 7:45
  • So as I have few Boolean variables in apex class which I am setting according to my need in my apex class constructor now how to achieve the same in LWC
    – Varun
    Commented Jul 2, 2020 at 7:47
  • State is automatically maintained for you in your Visualforce controller by transferring the "page state" between the client and server. For LWC there's nothing automatic here; you have to explicitly pass all relevant data from the client to the server with each method call.
    – Phil W
    Commented Jul 2, 2020 at 7:49
2

If you want to call Apex on component loading then use the below code

@wire(fetchConRec, ({contactId: '$conId'})) 
 wiredRecord({error,data}) {
 if (data) {
   console.log(data);
 } else if (error) {
     console.log(error);
  }
}

otherwise

fetchConRec(contactId: this.conId)
 .then(result=> {
        console.log('result-->',result);
  })
 .catch(error => {
        console.log('error-->',error);
  });

contactId is Apex class method's parameter conId is JS file variable name

2
  • I wanted to know like in Visualforce it will first call the constructor on above mentioned code <apex:page Controller="ClassName" > so how to call the same apex class constructor from the LWC.?
    – Varun
    Commented Jul 2, 2020 at 7:42
  • You cannot call a constructor (at least not directly). See my answer - you need to refactor your server-side code.
    – Phil W
    Commented Jul 2, 2020 at 7:53
2

I'm having the same case where I want to access an instance of Apex controller class in LWC. Here's how I did it:

In apex code

@AuraEnabled
    public static YourController getController() {
        return new YourController (true);
    }

In javascript code

import getController from "@salesforce/apex/YourController.getController";

controller;

async connectedCallback() {
  this.controller = await getController();
}

Hope it helps.

You must log in to answer this question.

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