You need to sign in to do that
Don't have an account?
Kyle 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)
Here is the query:
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:
Any one know what we are doing wrong? I'm scratching my head on this one.
Any help would be greatly appreciated!
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)
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!
All Answers
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
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?
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
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 ],