2

ISSUE


The parameter name of my Apex method did not match the name of the variable when I called the method from JavaScript.

ORIGINAL QUESTION


I've never touched frontend before - I have no clue what I'm doing, so please bear with me!

So, I am trying to pass some data from a Lightning Web Component to a method in Apex.

I have console.log()'d the data just before it gets passed and it looks fine - when referencing the parameter in Apex though, it's null!

Here's the JavaScript:

1    updateBooleanSearchQuery() {
2        if (this.filters.length !== 0) {
3            let objectApiName = this.objectApiName;
4            let filters = JSON.stringify(this.filters);
5            console.log(filters);
6            getBooleanSearchText({ objectApiName, filters })
7                .then(result => {this.booleanSearchFilters = result});
8        }
[...]

console.log() on line 5 is printing:

[{"label":"Active__c : True","key":0,"value":"True","field":"Active__c"}]

But as soon as the value enters my Apex method:

1    @AuraEnabled
2    public static String getBooleanSearchText(String objectApiName, String jsonFilters) {
3        System.debug(jsonFilters);
[...]

I get this debug:

[230]|DEBUG|null



The most confusing part to me is that the exact same thing happens elsewhere and works perfectly...

1    search() {
2        this.isLoaded = !this.isLoaded;
3        console.log(this.postcode);
4        console.log(this.distance);
5        let postcode = this.postcode;
6        let distance = this.distance;
7        let objectApiName = this.objectApiName;
8        let filters = JSON.stringify(this.filters);
9        console.log(JSON.parse(JSON.stringify(this.filters)));
10        // this.updateBooleanSearchQuery();
11        searchForRecords({ objectApiName, postcode, distance, filters })
12            .then(result => {console.log(result); this.searchResults = result; this.isLoaded = false; this.resultsTitle = 'Results Found: ' + result.length;})
13            .catch(error => {console.log(error); this.error = error;});
14    }
1    @AuraEnabled
2    public static List<RadiusList> execute(String objectApiName, String postcode, String distance, String filters) {
3        System.debug(filters);
[34]|DEBUG|[{"label":"Active__c : True","key":0,"value":"True","field":"Active__c"}]

What is causing it to be null?

Please let me know if any more information is needed, I can't figure this out at all!!

7
  • 4
    Your Apex parameters must be named exactly as stated in Javascript. Compare filters vs. jsonFilters.... Jun 24 at 19:20
  • 2
    @FelixvanHove Thank you so much! It works perfectly now. I've been so lost on this for ages now, I'm so glad it was this simple. Thank you so so much Jun 24 at 19:30
  • 1
    @FelixvanHove : You should add that as an answer.
    – Moonpie
    Jun 24 at 19:37
  • Nico, your "Solved by..." is very kind and I appreciate it. But please remove it, it's unnecessary and not common here. Not unlikely you can help others out here too, not unlikely I will one day ask something and you know the answer :-) Jun 24 at 19:47
  • 1
    @FelixvanHove my apologies! I have removed it now :) Jun 25 at 18:21

1 Answer 1

6

Your Apex parameters must be named exactly as stated in Javascript. Compare filters with jsonFilters.

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.