+ Start a Discussion
Kyle HavelkaKyle Havelka 

Issue with calling Apex Controller from Lightning web component.

Hi All,

Let me preface this with a disclaimer. I am fairly new to SalesForce development. But I have 11 years experience as a developer.

I am working on a LWC that calls an Apex controller.

The Apex controller runs a fairly simple SOQL query against a custom object Move_Type__c. It has a master detail relationship with another Custom object Move_Goal_Type__c (see below)

Move_Type & Move_Goal_Type

Here is the query: 
[SELECT Name, Id
FROM pursue02__Move_Type__c
WHERE pursue02__Move_Goal_Type__r.Id = :movegoalTypeId ]
The variable comes into the controller (MoveTypeController) as an Id through a method getMoveTypesByMoveGoalTypeId which is decorated with @AuraEnabled(cacheable=true)

When I execute anonynmous, it works as expected, I get a list of Move_Type__c records with the Name and Id.

When the LWC calls it, it returns an empty array. Now I have debugged the method System.debug(movegoalTypeId) and it shows the correct value. And I assigned the SOQL results to a variable and output that as well, it shows values in the debug logs.

I have also checked the network traffic via chrome developer tools and it's sending the value as expected:
message: {
    "actions": [
        {
            "id": "815;a",
            "descriptor": "aura://ApexActionController/ACTION$execute",
            "callingDescriptor": "UNKNOWN",
            "params": {
                "namespace": "pursue02",
                "classname": "MoveTypeController",
                "method": "getMoveTypesByMoveGoalTypeId",
                "params": {
                    "goalTypeId": "a013k00000aWHa6AAG"
                },
                "cacheable": true,
                "isContinuation": false
            }
        }
    ]
}
But as I said it returns an empty array:
"actions": [
        {
            "id": "815;a",
            "state": "SUCCESS",
            "returnValue": {
                "returnValue": [],
                "cacheable": true
            },
            "error": []
        }
    ],
We have checked the permissions on both Move_Type__c and Move_Goal_Type__c, and they all appear to be correct.

Any one know what we are doing wrong? I'm scratching my head on this one.

Any help would be greatly appreciated!

 
Best Answer chosen by Kyle Havelka
Alain CabonAlain Cabon
  • Be careful to the used character case for the parameter goalTypeId even if Apex is not case sensitive but use preferably exactly the same values for the  parameters by copying/pasting the value between javascript and apex for the method name and the parameters.
  • You can try without cacheable=true

All Answers

Khan AnasKhan Anas (Salesforce Developers) 
Hi Kyle,

Greetings to you!

Are you using the Wire Service to get data? I request you please post the complete code snippet of what you have tried so that we can look into it and can help you accordingly. I would suggest you please refer to the below links which might help you further.

https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.apex

https://rajvakati.com/2018/12/28/call-apex-methods-in-lightning-web-components/

https://www.salesforcecodecrack.com/2019/01/how-to-invoke-apex-class-from-lightning.html

http://amitsalesforce.blogspot.com/2018/12/Invoke-Apex-Controller-from-Lightning-Web-Component.html

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Alain CabonAlain Cabon
  • Be careful to the used character case for the parameter goalTypeId even if Apex is not case sensitive but use preferably exactly the same values for the  parameters by copying/pasting the value between javascript and apex for the method name and the parameters.
  • You can try without cacheable=true
This was selected as the best answer
Kyle HavelkaKyle Havelka
Thanks everyone for your suggestions! Alain your first suggestions was right on the money! Boy do I feel dumb! I was passing goalTypeId from the LWC, and in the controller the paramater was moveGoalTypeId.

When I changed it to goalTypeId, it started working (see repsonse below).

I am still baffled as to why when I put the debug statements into the controller it showed the Id, and also showed responses, but returned nothing.
Maybe there is a hidden contract that checks to see if the parameter from LWC matches the parameter in the Apex controller?
"actions": [
        {
            "id": "808;a",
            "state": "SUCCESS",
            "returnValue": {
                "returnValue": [
                    {
                        "MoveTypeId": "a023k00000VjgGhAAJ",
                        "Name": "Super Saturday Attendance Taker"
                    },
                    {
                        "MoveTypeId": "a023k00000VjgGcAAJ",
                        "Name": "Take Attendance at Event"
                    }
                ],
                "cacheable": true
            },
            "error": []
        }
    ],
Alain CabonAlain Cabon
Hi Kyle,

I had had many times the same problem and often, it is almost invisible even when we know the trick (and no warnings anywhere) as long as we don't copy/paste the exact values (it is a very specific rule for this type of call with Aura/LWC).

Best regards.

Alain
Daniel Barry 9Daniel Barry 9
I'm not sure, but the semicolon (;) in the "id": "808;a" may be hindering it somehow.. 

01"actions": [
02        {
03            "id": "808;a",
04            "state": "SUCCESS",
05            "returnValue": {
06                "returnValue": [
07                    {
08                        "MoveTypeId": "a023k00000VjgGhAAJ",
09                        "Name": "Super Saturday Attendance Taker"
10                    },
11                    {
12                        "MoveTypeId": "a023k00000VjgGcAAJ",
13                        "Name": "Take Attendance at Event"
14                    }
15                ],
16                "cacheable": true
17            },
18            "error": []
19        }
20    ],