+ Start a Discussion
JonathanFox UKJonathanFox UK 

LWC wont call Apex Method

Hi All,

Here I have a Batch Apex class which uses an API to get some random contacts. This all works perfectly fine and I am happy with it,

I wanted to trigger this with a user input via a LWC. I created BatchCreateContactController to do this.

Calling BatchCreateContactController from the Developer sonsole works, but my LWC won't call it. I suspect it is something to do with the LWC JS file. Unsure.
Best Answer chosen by JonathanFox UK
Alain CabonAlain Cabon
The call of an Apex method imperatively with parameters is incomplete in the documentation indeed (they refer to the LWC recipes for an example).
 
apexImperativeMethodWithParams.js :

https://github.com/trailheadapps/lwc-recipes/blob/master/force-app/main/default/lwc/apexImperativeMethodWithParams/apexImperativeMethodWithParams.js


BatchCreateContactController.class :

public static Id initiateContactBatch(Integer numberOfRecords) {
 
createRecords(){
        
        initiateContactBatch({ numberOfRecords: this.numberInput })
            .then((result) => {
                this.jobId = result;   
                this.error = undefined;      
            })
            .catch((error) => {
                this.error = error;
                this.jobId = undefined;
            });
}
The LWC recipe:
handleSearch() {
        findContacts({ searchKey: this.searchKey })
            .then((result) => {
                this.contacts = result;
                this.error = undefined;
            })
            .catch((error) => {
                this.error = error;
                this.contacts = undefined;
            });
    }

That is a common error because of the missing example in the documentation of LWC (the problem of the parameter passed by position only, not sufficient here).

All Answers

Alain CabonAlain Cabon
The common error is that you don't use the exact values of the names of the parameters used in the Apex method for the call in LWC.

The position of the parameter in the method is not enough. The names of the parameters must have also exactly the same values in LWC (including the case (lower-upper)).

We can't see your code (without code, there will be few answers for your question).
 
JonathanFox UKJonathanFox UK
My appologies, here is the repo https://github.com/JFoxUK/JFoxUK-Dev
Alain CabonAlain Cabon
The call of an Apex method imperatively with parameters is incomplete in the documentation indeed (they refer to the LWC recipes for an example).
 
apexImperativeMethodWithParams.js :

https://github.com/trailheadapps/lwc-recipes/blob/master/force-app/main/default/lwc/apexImperativeMethodWithParams/apexImperativeMethodWithParams.js


BatchCreateContactController.class :

public static Id initiateContactBatch(Integer numberOfRecords) {
 
createRecords(){
        
        initiateContactBatch({ numberOfRecords: this.numberInput })
            .then((result) => {
                this.jobId = result;   
                this.error = undefined;      
            })
            .catch((error) => {
                this.error = error;
                this.jobId = undefined;
            });
}
The LWC recipe:
handleSearch() {
        findContacts({ searchKey: this.searchKey })
            .then((result) => {
                this.contacts = result;
                this.error = undefined;
            })
            .catch((error) => {
                this.error = error;
                this.contacts = undefined;
            });
    }

That is a common error because of the missing example in the documentation of LWC (the problem of the parameter passed by position only, not sufficient here).
This was selected as the best answer
JonathanFox UKJonathanFox UK
Ahhh that makes lots of sense. Thank you so so much!