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?

  • 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 Jul 2 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 Jul 2 at 7:41
3

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.

| improve this answer | |
  • 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 Jul 2 at 7:42
  • 1
    @Varun you can't – zaitsman Jul 2 at 7:42
  • As I said, there's no controller and LWC can only invoke static methods. – Phil W Jul 2 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 Jul 2 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 Jul 2 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

| improve this answer | |
  • 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 Jul 2 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 Jul 2 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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